Could we have `inline fun <reified T : Any> ...
# stdlib
m
Could we have
inline fun <reified T : Any> Any?.cast(): T = this as T
in the stdlib? It's been in addToStdLib.kt for a while but looks like it's now deprecated? Is there a fundamental reason this is not possible?
โž• 2
๐Ÿ‘๐Ÿผ 1
d
There is a reasoning described in the same file
Copy code
Usage of this function is unsafe because it does not have native compiler support
This means that compiler won't report UNCHECKED_CAST, CAST_NEVER_SUCCEED or similar diagnostics in case of error cast (which can happen immediately or after some refactoring of class hierarchy)
Consider using regular `as` and `as?`
m
This is what I'm not getting. The function is doing the same thing as what I would normally do from my code. Am I missing on diagnostics when doing this myself?
And follow up question is could compiler support be added?
d
This function allow such code to be compiled without any warnings/errors
Copy code
"hello".as<Int>()
๐Ÿ‘๐Ÿพ 1
๐Ÿ‘ 5
m
Ahhh yes, I see ๐Ÿ‘
d
And follow up question is could compiler support be added?
It could, but it's not on the table right now
m
Gotcha ๐Ÿ‘
Thanks for the insights!
๐Ÿ‘Œ 1
a
Yes, thats some good insight