https://kotlinlang.org logo
Title
v

Viktor Qvarfordt

11/20/2019, 10:02 AM
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

Joffrey

11/20/2019, 1:13 PM
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

Viktor Qvarfordt

11/20/2019, 1:39 PM
Good thinking, but this was not the case. I really wonder how to get this working 🤔
j

Joffrey

11/20/2019, 3:10 PM
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

Viktor Qvarfordt

11/20/2019, 3:16 PM
In principle, my code should behave just like ordinary .map, hopefully someone with deeper understanding of type inference catches this :)
j

Joffrey

11/20/2019, 4:23 PM
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

Kroppeb

11/21/2019, 4:16 PM
This works fine for me.
v

Viktor Qvarfordt

11/23/2019, 12:08 PM
@Kroppeb Interesting! That is precisely the code I have. What version of Kotlin and IntelliJ are you running?
k

Kroppeb

11/23/2019, 12:14 PM
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

Viktor Qvarfordt

11/23/2019, 12:15 PM
Thanks, I’ll have a look.
k

Kroppeb

11/28/2019, 6:46 PM
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

Viktor Qvarfordt

12/07/2019, 12:49 PM
🤔 ok so this cannot be relied upon it seems