Am I not using higher-ordered-functions properly o...
# announcements
v
Am I not using higher-ordered-functions properly or is Kotlin’s type inference limited? (This is a silly example where I’m trying to get a “flow” or “left compose” operator - I know this is most natural with dot chaining in Kotlin.)
j
I'm not sure how the associativity works in your case, but I would say the error would make sense if the right-most expression is evaluated first. Try putting
()
around to see if that's the actual issue 😉
v
Good thinking, but this was not the case. I really wonder how to get this working 🤔
j
Actually I think
map { it < 2 }
may still be evaluated independently, despite your
()
, so maybe my initial solution was not a solution, but the problem may still be this one
You have to specify the type parameters explicitly for
map
, or maybe giving a type to the lambda's param is sufficient:
map { i: Int -> i + 2 }
. I'll let the smart guys answer the why :)
v
In principle, my code should behave just like ordinary .map, hopefully someone with deeper understanding of type inference catches this :)
j
I believe type inference is a bit harder for top-level functions like yours compared to functions with receivers like the stdlib's
map
k
This works fine for me.
v
@Kroppeb Interesting! That is precisely the code I have. What version of Kotlin and IntelliJ are you running?
k
I did an update yesterday right before that. So kotlin 1.3.60 and latest intelliJ. But I didn't think the type inference got improved. Does your code compile if you try. Cause if compiling works then it might have been an issue with the ide that got fixed.
v
Thanks, I’ll have a look.
k
When I wanted to reuse the scratch file for a different problem I noticed that my ide is now complaining again
1.3.61
v
🤔 ok so this cannot be relied upon it seems