On Friday, 19 February 2016 at 12:16:49 UTC, Jacob Carlborg wrote:
On 2016-02-19 00:33, Yuxuan Shui wrote:
Just come across Kotlin today, and found some interesting ideas skimming
through its tutorial:

1) Null check
[snip]

You can even do this:

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

I think the same applies to Swift. But I think you're supposed to use it like this:

if let a = var {
  // a is no unwrapped
}

But you would need to give the unwrapped value a new name.


I like the way it's used in Scala:

val a = Option(value)
val b = a.map(e => e /* do something with the unrapped e).getOrElse(/* use a default value here */)

The Option type in Scala acts like a zero or one element collection. The Scala way can be implemented in D as well without any language support.

2) Smart cast
[snip]

It's similar how it works in D, for Objects:

class Foo {}
Object foo = new Foo;

if (auto o = cast(Foo) foo)
    // use o as Foo here

When casting to a subclass it will return null reference if the cast fails.

The point is, in other languages you would have to explicitly do an operation to the wrapped value, but in Kotlin the compiler is able to automatically unwrap it for you when it knows doing so is legal, based on static analysis results.

Reply via email to