I have two methods: ``` fun <T> bind(type: C...
# announcements
d
I have two methods:
Copy code
fun <T> bind(type: Class<T>, instance: T)
fun <T> bind(type: Class<T>, factory: Function<T>)
Calling it like this fails with "Cannot choose among the following candidates without completing type inference":
bind(MyClass::class.java, { p: String -> MyClass(p) })
Why is this? Using a reference instead of a lambda (
bind(MyClass::class.java, ::MyClass)
) works fine. What am I missing here?
a
The compiler tries to infer the shape (ins and outs) of the lambda before it evaluates the body of the lambda
d
Yes. And that should tell it that the lambda returns
MyClass
, no?
a
and because it doesn't know the shape it cannot tell if its a
T
or a
Function<T>
d
Well, it can't be a
T
, since
T
is
MyClass
...
I guess I'll have to rename one to
bindInstance
😞
a
d
Thank you!
a
there are a couple of bugs because of how the compiler handles method resolution with lambdas involved
what type is your
Function<T>
? the java one?
d
No,
kotlin.Function
a
any reason you are using
kotlin.Function
rather than a Function Type?
oh and btw, if you tell the compiler what
T
is, your code works
bind<MyClass>(MyClass::class.java, ...)
(but then you have
MyClass
3 times in the same line 😬
d
The reason I am using
kotlin.Function
is because I want to be able to pass a function with any number of parameters.