[OT] Some neat ideas from the Kotlin language

Tobias Müller via Digitalmars-d digitalmars-d at puremagic.com
Sat Feb 20 01:40:40 PST 2016


Yuxuan Shui <yshuiv7 at gmail.com> wrote:
> 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

In Rust that would be:

let var : Option<i32> = ...;
if let Some(var) = var {
    // You can use var here
}

It works for every enum (= tagged union), not just Option<T>

Swift also has "if let".

It's not much more verbose but more explicit.
Changing the type of a variable based on static analysis is just advanced
obfuscation. It hurts readability and the gain is questionable. At least it
only works for nullable types.

Tobi


More information about the Digitalmars-d mailing list