Lulu
11/18/2020, 8:47 PMMutableList
, and I expose it to another function with just the List
interface, can that function cast it to MutableList
? At what point is something cast-able to its original type? I'm not new to generics but I haven't thought about that before.ephemient
11/18/2020, 8:50 PMephemient
11/18/2020, 8:52 PMLulu
11/18/2020, 8:54 PMasList()
exist, to copy it into another new instance?ephemient
11/18/2020, 8:55 PMephemient
11/18/2020, 8:55 PMephemient
11/18/2020, 8:56 PMlistOf(1, 2, 3) as MutableList<Int>
succeedsephemient
11/18/2020, 8:56 PMephemient
11/18/2020, 8:56 PMkotlin.collections.List
and kotlin.collections.MutableList
map to java.util.List
)Shawn
11/18/2020, 8:57 PMList
to a more concrete type are breaking their end of the API contract and are treading into “undefined behavior”-style waters anyhowShawn
11/18/2020, 9:01 PMUnmodifiableList
and they try to cast it to a MutableList
they’ll get a nice little runtime error unless they cast with as?
and handle the null somehowephemient
11/18/2020, 9:03 PMUnmodifiableList(...) is MutableList<*>
Shawn
11/18/2020, 9:03 PMNir
11/18/2020, 9:03 PMList
that's "really" a MutableList, somebody else can just mutate itShawn
11/18/2020, 9:04 PMephemient
11/18/2020, 9:04 PMNir
11/18/2020, 9:04 PMList
this is less of an issue but if you are a class with a List
member, then you need to be aware of itNir
11/18/2020, 9:05 PMephemient
11/18/2020, 9:05 PMLulu
11/18/2020, 9:05 PMShawn
11/18/2020, 9:05 PMephemient
11/18/2020, 9:05 PMNir
11/18/2020, 9:05 PMNir
11/18/2020, 9:06 PMephemient
11/18/2020, 9:06 PMephemient
11/18/2020, 9:06 PMNir
11/18/2020, 9:07 PMNir
11/18/2020, 9:07 PMNir
11/18/2020, 9:07 PMephemient
11/18/2020, 9:08 PMephemient
11/18/2020, 9:08 PMNir
11/18/2020, 9:09 PMNir
11/18/2020, 9:09 PMephemient
11/18/2020, 9:11 PMephemient
11/18/2020, 9:12 PMNir
11/18/2020, 9:13 PMNir
11/18/2020, 9:13 PMList
should really be returning ImmutableList
instead, but I guess like you say it isn't done for Java overhead reasonsephemient
11/18/2020, 9:14 PMNir
11/18/2020, 9:14 PMimport kotlinx.immutable.stdlib_extensions.*
or something like thatNir
11/18/2020, 9:15 PM+
and map
return ImmutableList instead of Listephemient
11/18/2020, 9:15 PMNir
11/18/2020, 9:15 PMNir
11/18/2020, 9:15 PMImmutableList
inherits from List
Nir
11/18/2020, 9:15 PMephemient
11/18/2020, 9:16 PMephemient
11/18/2020, 9:16 PMNir
11/18/2020, 9:19 PMImmutableList
to MutableList
using as
you'll get an exception at runtimeNir
11/18/2020, 9:19 PMNir
11/18/2020, 9:21 PMclass ImmutableList<E>(val x: List<E>): List<E> by x
afaics casting ImmutableList to MutableList
gives an error the same as casting ImmutableList to Intephemient
11/18/2020, 9:22 PMephemient
11/18/2020, 9:22 PMephemient
11/18/2020, 9:23 PMNir
11/18/2020, 9:24 PMShawn
11/18/2020, 9:25 PMNir
11/18/2020, 9:26 PMShawn
11/18/2020, 9:26 PMephemient
11/18/2020, 9:26 PMNir
11/18/2020, 9:26 PMephemient
11/18/2020, 9:26 PMNir
11/18/2020, 9:26 PMephemient
11/18/2020, 9:27 PMNir
11/18/2020, 9:27 PMNir
11/18/2020, 9:27 PMNir
11/18/2020, 9:29 PMephemient
11/18/2020, 9:29 PMNir
11/18/2020, 9:29 PMNir
11/18/2020, 9:29 PMephemient
11/18/2020, 9:32 PMNir
11/18/2020, 9:35 PMephemient
11/18/2020, 9:37 PMephemient
11/18/2020, 9:40 PMephemient
11/18/2020, 9:40 PMNir
11/18/2020, 9:41 PMNir
11/18/2020, 9:42 PMNir
11/18/2020, 9:43 PMmap
to return ImmutableList
is not "really" incompatible with other Kotlin code, ImmutableList
is still a sub-type of List
so it should work. The only code it will break is naughty code that is downcasting and operating on the MutableListNir
11/18/2020, 9:44 PMephemient
11/18/2020, 9:45 PMNir
11/18/2020, 9:45 PMephemient
11/18/2020, 9:45 PMTypeIntrinsics.asMutableList()
ephemient
11/18/2020, 9:46 PMNir
11/18/2020, 9:50 PMephemient
11/18/2020, 9:56 PMephemient
11/18/2020, 9:56 PMephemient
11/18/2020, 9:56 PMArkadii Ivanov
11/19/2020, 12:05 AMList
can always be casted to MutableList
only in JVM. And if you write your own implementation of the List
interface (not MutableList
), then any attempt to mutate the list will result in NotImplementedError
being thrown. In non-JVM targets the cast may fail at runtime. I think this happens in Playground, because it is probably based on Kotlin/JS. https://pl.kotl.in/xWXTWNGzmephemient
11/19/2020, 12:08 AM