groostav
06/16/2024, 3:38 AMArray<Nothing>
in kotlin. I understand that Array
is not a real class and thus kotlins presentation of Array
as a generic type is misleading and I'm certain the reason that Array<Nothing>
being a Nothing[]
and something to do with reifying the type nothing is the cause, but I cant understand why. Is there no entry in the classpool for Nothing
?ephemient
06/16/2024, 3:41 AMArray<Nothing>?
is representable on JVM (as [Ljava/lang/Void;
) but Array<Nothing>
is not (there is no [V
)ephemient
06/16/2024, 3:43 AMephemient
06/16/2024, 3:44 AMgroostav
06/16/2024, 3:46 AMUnit
is more like Void
,
so Array<Unit?>
-> jbc -> java == Void[]
makes sensegroostav
06/16/2024, 3:47 AMNothing
and Void
share much in commonephemient
06/16/2024, 4:01 AMvoid
has two meanings, one is Unit
-like and one is Nothing
-likeephemient
06/16/2024, 4:04 AM() -> Nothing
has a similar meaning as Callable<@Nonnull Void>
, for example - meaning it cannot return normally
definitely a different meaning than () -> Unit
which is somewhat like Callable<@Nullable Void>
which can return normally, only with a single valueephemient
06/16/2024, 4:13 AMClass<Nothing>
like Class<*>
a raw type. (conversely, generics in raw types become in Nothing
when in
parameterized and out Any?
when in
parameterized, https://kotlinlang.org/docs/generics.html#star-projections).
so under those rules, it's understandable that Array<Nothing>
doesn't work, as only reified array types existjw
06/16/2024, 4:17 AMArray<Nothing>
anyway? Represent a zero-length array separately from a non-zero-length array?groostav
06/16/2024, 4:50 AMEMPTY
instance, which is a MyCustomCollection<Nothing>
, which wants an instance to fill it's backing array.groostav
06/16/2024, 4:54 AMjw
06/16/2024, 4:55 AMephemient
06/16/2024, 6:48 AM<T>
, because Array<Any>
cannot be cast to Array<AnyOtherType>
ephemient
06/16/2024, 6:49 AMArray
, even if you could write Array<Nothing>
. why are you using Array
anyway? almost always it's better to work with List
or other collectionsFleshgrinder
06/16/2024, 8:53 AMNothing
isn't void
nor Void
. It's nothing. It cannot be stored, not in an array, not elsewhere. Hence, the fact that Array<Nothing>
isn't valid makes perfect sense. Array<Nothing?>
should work, but that's captured in KT-67675. Imho Kotlin should feature Null
as a dedicated type for use cases like this and other where only null
is permissible.ephemient
06/16/2024, 9:23 AMFleshgrinder
06/16/2024, 9:40 AMArray<Null>
would translate to Void[]
and thus be representable. In this particular case it doesn't matter to the Kotlin user what the native type is, all they want is null
.groostav
06/29/2024, 3:23 AMList<Nothing>
which is a helpful type and is exactly what is returned by emptyList
As I think about it I wonder if kotlinc (and kotlin 2.0) could pull the trigger on --what I hope is a fairly small-- implementation of type-erasure arrays. If the language presents them as generic, then why not use the traditional generic facilities?
Something like val myArray: Array<Customer> = Array<@NotReified Customer>(3) { ... }
. What @NotReified
would do is tell the compiler to just use Object[]
. It of course comes with all the caveats of heap pollution but those exist for any generic type.
thoughts? should kotlin just sorta, not-do arrays and use Object[]
as the backing type for any array type (--as is the employed solution for java collections)?ephemient
06/29/2024, 9:35 AMArray<String>
to a Java method expecting Array<String>
if it was created as an Array<@NotReified String>