The following little code produces an error in IntelliJ IDEA.
class A(fct1: (Int) → Float = {0.4f}) {
constructor(fct2:(Int)->Unit) : this()
}
The compiler perceives fct1 and fct2 as a conflict, allthough they have different types (Int)->Float and (Int)->Unit respectively. Isn’t it the philosophy of Kotlin to take care for exactly that sort of thing? fct1 and fct2 are just different parameters.
Any opinion?
1 Like
Maybe it works like this:
class A(...){
companion object {
operator fun invoke(...)= A()
}
}
the reason it doesn’t work is that the return type of a lambda is a generic.
Generics are changed to object during compile time.
Therefor, both the lambda’s will return the same value: Any?
.
Therefor it’s not possible to have both at the same time.
With my solution, you will have two functions that are really different.
That means that it’s now a question of kotlin is smart enough to create the correct match…
Update: Kotlin can’t make the match (yet), even when inlining both.
I Java you can have very similar problem
import java.util.function.Function;
public class A {
public A(Function<Integer, Float> f) { }
public A(Function<Integer, Void> f) { }
}
Error: ‘A(Function<Integer, Float>)’ clashes with ‘A(Function<Integer, Void>)’; both methods have same erasure