https://kotlinlang.org logo
Title
e

epool

08/20/2018, 6:53 PM
this code doesn’t complain in compilation time but in runtime if I don’t specify the type of
classLoaders: Array<out ClassLoader>?
when
*classLoaders
is passed as parameter
val classLoaders = reflections.configuration.classLoaders // Array<out ClassLoader>?
ReflectionUtils.forNames<Any>(reflections.allTypes, *classLoaders)
a

Andreas Sinz

08/20/2018, 7:01 PM
What does it say at runtime?
e

epool

08/20/2018, 7:24 PM
NPE
p

pavel

08/20/2018, 7:54 PM
When you are interacting with platform code that has no Nullable annotations, Kotlin lets you decide whether the type is nullable or not and when you don’t do that it decides based on context.
In this case, you are using the
*
operator which only works with non-null arrays
since your array appears to be null it throws a NPE
if you declared
classLoaders: Array<out ClassLoader>?
you would’ve gotten a compilation error there instead
p

poohbar

08/20/2018, 8:17 PM
And this is why we should never promote Kotlin as a null-safe language because it's only as null-safe as it's platform i.e. not at all.
p

pavel

08/20/2018, 8:30 PM
well, safety is relative. You can always do unsafe things like using platform types or casting, but outside of that you are pretty much safe.
Kotlin makes it harder to shoot yourself in the foot than Java, but you can never remove all of the foot-pointing guns. Even Haskell has
System.IO.Unsafe
k

karelpeeters

08/20/2018, 9:43 PM
@poohbar as long as you stay withing Kotlin it's pretty much null-safe.
So "Kotlin is null safe" is true, but since Java is not of course calling Java code isn't either.
p

poohbar

08/21/2018, 12:10 PM
stay withing Kotlin
How many real-world projects can get away with not using Java?
e

epool

08/21/2018, 4:00 PM
actually
reflections.configuration.classLoaders
is annotated with a
@javax.annotation.Nullable
return type, I thought this could be inferred.
@Nonnull
is supported
@Nullable
isn’t
e

epool

08/21/2018, 5:58 PM
thank you @pavel
👍 1
a

Andreas Sinz

08/22/2018, 12:46 PM
@poohbar only use nullable types when interfacing with java and you have full null-safety
p

poohbar

08/22/2018, 12:47 PM
Since the compiler does not force you to do that it's like saying that Java is null-safe if you do a null check before accessing any reference.
a

Andreas Sinz

08/22/2018, 12:48 PM
at one point, kotlin did exactly that, but it was removed because a lot of developers were not happy with it
p

poohbar

08/22/2018, 12:49 PM
I know the history of Kotlin. And I also know that once this was removed Kotlin ceased to be a null-safe language and we should stop saying it is.
I am not saying I have a better solution though.
k

karelpeeters

08/22/2018, 1:07 PM
Kotlin is null-safe, the interaction with Java is not. Do you also say that Java is not memory safe because it can interact with the JNI?