nullable - Make Kotlin warn on assignment of flexible/platform type to non-null type? -


when calling non-nullability-annotated java function kotlin, flexible-typed return values, denoted exclamation marks, e.g. string!.

kotlin silently allows assigning these flexible values normal non-null type, e.g. string, can cause nullpointerexceptions @ runtime.

i prefer compiler warnings or errors such assignments. alternatively, treat platform types equivalent nullable types (e.g. string?).

as example, java code:

import android.os.systemclock; import android.support.annotation.nonnull; import android.support.annotation.nullable;  public class nulltest {      private string maybe() {         if (systemclock.elapsedrealtimenanos() % 2 == 0) {             return null;         }         return "ok";     }      public string annotatednothing()            { return maybe(); }     @nullable public string annotatednullable() { return maybe(); }     @nonnull  public string annotatednonnull()  { return "ok"; }  } 

...and following kotlin code, i'd errors on 2 new lines (see comments):

fun testnulls() {     val obj = nulltest()      val nullexact: string  = obj.annotatednullable() // gives error     val nullmaybe: string? = obj.annotatednullable()     val nullinfer          = obj.annotatednullable()      val okayexact: string  = obj.annotatednonnull()     val okaymaybe: string? = obj.annotatednonnull()     val okayinfer          = obj.annotatednonnull()      val bareexact: string  = obj.annotatednothing() // want compiler error here     val baremaybe: string? = obj.annotatednothing()     val bareinfer          = obj.annotatednothing()      print("length " + nullexact.length)     print("length " + nullmaybe.length) // gives error     print("length " + nullinfer.length) // gives error      print("length " + okayexact.length)     print("length " + okaymaybe.length) // gives error     print("length " + okayinfer.length)      print("length " + bareexact.length)     print("length " + baremaybe.length) // gives error     print("length " + bareinfer.length) // want compiler error here } 

the point force me add null checks or !!, making sure @ least have explicit it.

is possible?

in comments of 2014 jetbrains blog post, when introduced platform/flexible types, sounds planning add option warn these situations, haven't been able find further information on that.

the kotlin documentation describes exact case:

any reference in java may null, makes kotlin's requirements of strict null-safety impractical objects coming java. types of java declarations treated specially in kotlin , called platform types. null-checks relaxed such types, safety guarantees them same in java

unfortunately, means there no way issue warning during compilation time. silver lining kotlin @ least prevent null propagation in runtime.

when call methods on variables of platform types, kotlin not issue nullability errors @ compile time, call may fail @ runtime, because of null-pointer exception or assertion kotlin generates prevent nulls propagating

if don't control source code of java library use, treat of results potentially nullable, unless evident not.


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -