Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions src/bugs/multiArity/Rationale.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
The correct answer is **b) 0**.

While IDE shows that “Check for instance is always 'true'” for both `is` expressions,
only first one will be successful.

The cause of [this bug](https://youtrack.jetbrains.com/issue/KT-24067) is that
instead of generating normal INSTANCEOF (or CHECKCAST)
compiler inserts a call to `kotlin.jvm.internal.TypeIntrinsics.isFunctionOfArity`
which internally uses `getFunctionArity` declared as follows:

```java
public static int getFunctionArity(Object obj) {
if (obj instanceof FunctionBase) {
return ((FunctionBase) obj).getArity();
}
else if (obj instanceof Function0) {
return 0;
}
else if (obj instanceof Function1) {
return 1;
}
else if (obj instanceof Function22) {
return 22;
}
else {
return -1;
}
}
```

Thus, Kotlin compiler expects functions to have only one arity.

16 changes: 16 additions & 0 deletions src/bugs/multiArity/multiArity.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package bugs.multiArity
// by Mike Gorünov @Miha-x64

object MultiFunction : () -> Unit, (Unit) -> Unit {
override fun invoke() {}
override fun invoke(p1: Unit) {}
}

if (MultiFunction is () -> Unit) print(0)
if (MultiFunction is (Unit) -> Unit) print(1)

// What will it print?
// a) nothing
// b) 0
// c) 1
// d) 01