diff --git a/src/bugs/multiArity/Rationale.md b/src/bugs/multiArity/Rationale.md new file mode 100644 index 0000000..a6d6267 --- /dev/null +++ b/src/bugs/multiArity/Rationale.md @@ -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. + \ No newline at end of file diff --git a/src/bugs/multiArity/multiArity.kts b/src/bugs/multiArity/multiArity.kts new file mode 100644 index 0000000..518b00d --- /dev/null +++ b/src/bugs/multiArity/multiArity.kts @@ -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