https://kotlinlang.org logo
#getting-started
Title
# getting-started
c

crummy

03/04/2021, 7:58 AM
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

Youssef Shoaib [MOD]

03/04/2021, 9:53 AM
Yeah this is because sadly contracts aren't featured enough right now to understand what that check actually means
1
b

Bornyls Deen

03/04/2021, 3:54 PM
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

Joel

03/04/2021, 6:29 PM
@Bornyls Deen technically
firstIsInstanceOrNull
🙂
👍 1
o

Orhan Tozan

03/07/2021, 12:51 AM
Or
firstInstanceOrNull
, or
findInstance