On 02/19/2016 07:16 AM, 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
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
Me want.
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
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.
It'd be nice if it didn't require a new variable name though. When I do
that, I usually find I wind up needing to resort to some
hungarian-notation-inspired "start including the type in the variable's
name" verbosity. It's a fantastic feature of D, but that Kotlin version
seems much nicer.