Is there a way to make this type of inference work...
# announcements
a
Is there a way to make this type of inference work?
I'm passing lambda ๐Ÿค”
Which is returning a
String
j
mmmm let me a momment
t
You need to either specify the generic argument of the function (
testInference<String> { ... }
) or that of the lambda's parameter (
testInference { a: String -> ... }
)
a
By the return type?
j
image.png
a
I know I could've do that, but it was implicitly known by the return type so I'd like to infer that ๐Ÿ™‚
j
I think I didn't see it working without indicating it, but probably there is an youtrack issue for this
Are you using Kotlin 1.4?
Well, it is failing for me with 1.4 too
a
Yes, 1.4.10
j
Maybe you should file a bug, it should be working
๐Ÿ‘ 1
j
btw, is there any reason to say:
<T: Any>
? shouldn't just
<T>
be enough? Or does it have to do with nullability?
a
@Joost Klitsie its not really related with the issue, though I am forcing lambda to return only non-nullable values.
j
Yeah I didn't mean for it to be related, but more wondering in general. But I see indeed what it does, forces T to be non null
a
Second works though first doesn't, i.e. if there is no argument with generic type T type is inferred, so it is capable to do so. Why doesn't in the first case ๐Ÿค”๐Ÿค”
a
The second one works because it doesn't need to infer the type of
it
a
@Afzal Najam I mean if the information is not available from
it
it should fallback to return type inferance, am I right?
a
Yes, for the first case, I think it should so that would be a bug
n
Does it work if it's T -> T
โž• 1
a
@Nir Didn't understood what you wanted to say ๐Ÿ˜…๐Ÿ˜…
n
You have lambdas with signatures T? to T
And Any? To T
I'm wondering about T to T
a
Ye, does not work (same as before)
n
Gotcha
a
In fact, this does not work here as well:
fun <R : T, T : Any> testInference(acc: (oldValue: R?) -> T){}
๐Ÿ˜•
n
Yeah must be a bug