voddan
09/28/2016, 6:56 AMfun boo(x: Any): List = x.cast()
voddan
09/28/2016, 6:57 AMstepango
09/28/2016, 7:14 AMtype cast
happened inside cast()
function, but i got your point, in some cases implementation i using have no guaranty you could actually make this castvmironov
09/28/2016, 7:41 AMcast
to the stdlib. It's one of the most popular extension functions in our project.michaelrocks
09/28/2016, 8:09 AMcast
as it’s an extremely dangerous function 🙂kirillrakhman
09/28/2016, 9:08 AMlist.firstOrNull()?.cast<Foo>().bar
kirillrakhman
09/28/2016, 9:08 AMlist.map(Any::cast)
kirillrakhman
09/28/2016, 9:09 AMkirillrakhman
09/28/2016, 9:10 AMcast()
could be made an intrinsic similar to arrayOfNulls()
and as
would be desugared to itkirillrakhman
09/28/2016, 9:11 AMfoo as Bar
and foo.cast<Bar>()
equalkirillrakhman
09/28/2016, 9:11 AM1 + 2
and 1.plus(2)
kirillrakhman
09/28/2016, 9:11 AMstepango
09/28/2016, 9:27 AM<http://foo.as|foo.as>(Bar::class)
wich would generate warningskevinmost
09/28/2016, 2:57 PMas?
and not as
. I'd rather have to use the safe-call operator than potentially hide an exception.
inline fun <reified R : Any> Any?.cast(): R? = this as? R
Usage: list.firstOrNull()?.cast<Foo>()?.bar
uhe
09/28/2016, 2:59 PMcastOrNull
uhe
09/28/2016, 3:00 PMfirst()
is also unsafe in that regard, which is why we have firstOrNull()
kevinmost
09/28/2016, 3:02 PMfirst()
because it's unsafe, but I guess it's more consistent to name them that waycedric
09/28/2016, 4:27 PMcast
syntax, I’d vote against it as well because it makes it harder to locate such casts in a general search in the code base. If I want to find out how many times I cast in my code, I can just do a search for ” as “
. Adding synonyms to this makes it harder to conduct this kind of search.cedric
09/28/2016, 4:28 PMvoddan
09/28/2016, 4:30 PMcedric
09/28/2016, 4:30 PMmichaelrocks
09/28/2016, 4:31 PMcedric
09/28/2016, 4:32 PMcedric
09/28/2016, 4:33 PM.cast()
over as
(one less keyword), but not today.michaelrocks
09/28/2016, 4:34 PMmichaelrocks
09/28/2016, 4:35 PMkirillrakhman
09/28/2016, 4:35 PMas
were sugar for a method, you could just use "find usages"cedric
09/28/2016, 4:35 PMcedric
09/28/2016, 4:36 PMas
as a keyword with IDEA instead of a text search?voddan
09/28/2016, 4:37 PM