[OT] Some neat ideas from the Kotlin language

Yuxuan Shui via Digitalmars-d digitalmars-d at puremagic.com
Thu Feb 18 15:33:45 PST 2016


Just come across Kotlin today, and found some interesting ideas 
skimming through its tutorial:

1) Null check

Kotlin has Optional types, suffixed with a '?'. Like 'Int?', same 
as in Swift. But instead of explicitly unwrapping them (e.g. var! 
in Swift, or var.unwrap() in Rust), Kotlin let you do this:


     var: Int?
     if (var != null)
         //You can use var here


Skipping the null check will get you compile time error.

You can even do this:

     if (var == null) return;
     //You can use var from now on

2) Smart cast

This is a similar to previous one, instead of:

     var1: Object;
     var2 = cast(String)var1;

You do this:

     if (var1 is String)
         //You can use var1 as a String here


I think this two ideas are pretty neat, for more information, see:

http://kotlinlang.org/docs/reference/typecasts.html


More information about the Digitalmars-d mailing list