Hey all. I'm new. I was fiddling around with gener...
# getting-started
j
Hey all. I'm new. I was fiddling around with generics and type inference using functional approaches, but I'm a little confused about type inference logic for second order functions. Given
fun <E> firstOrderFnWithSecondOrderTypeInference() = { value: E -> value }
, attempting to
firstOrderFnWithSecondOrderTypeInference()(42)
gives me a type inference error:
Type inference failed: Not enough information to infer parameter E in  fun <E> firstOrderFnWithSecondOrderTypeInference( ) : (E) → E Please specify it explicitly.
firstOrderFnWithSecondOrderTypeInference<Int>()(1234)
works because there's no type inference for
E
of the second rank function. I guess what I'm looking for is a why it's like this so I can understand the language a bit better. I know that C++ is the same way, same with Typescript, so I wonder if there's an underlying reason.
p
I think it is not about second order functions but about call chains. For example here compiler cannot infer type too:
Copy code
mutableListOf().add(1)
But maybe compiler will be smarter with new type inference (experimental in 1.3):

https://youtu.be/MyljSWm0Y_k?t=1932

👍 1
j
Oh wow! Well there you go. Thank you. This is a really good video!
👍 1