how come this doesn't smartcast the result of firs...
# getting-started
c
how come this doesn't smartcast the result of first to a String?
Copy code
val l = listOf<Any>("foo")
l.first { it is String}.length // unresolved reference: length
And is there a way to get this to work with a smartcast?
I found this way:
Copy code
l.filterIsInstance<String>().first().length
2
y
Yeah this is because sadly contracts aren't featured enough right now to understand what that check actually means
1
b
https://kotlinlang.slack.com/archives/C0B8Q383C/p1590610586096000 You might find something like this useful if you're having to do this kind of thing a fair bit 🙂
🤔 1
j
@Bornyls Deen technically
firstIsInstanceOrNull
🙂
👍 1
o
Or
firstInstanceOrNull
, or
findInstance