Jump to page: 1 2 3
Thread overview
[OT] Some neat ideas from the Kotlin language
Feb 18, 2016
Yuxuan Shui
Feb 19, 2016
Jacob Carlborg
Feb 19, 2016
Nemanja Boric
Feb 19, 2016
Thiez
Feb 21, 2016
Jacob Carlborg
Feb 19, 2016
Nick Sabalausky
Feb 19, 2016
Yuxuan Shui
Feb 20, 2016
Daniel N
Feb 20, 2016
Tobias Müller
Feb 20, 2016
rsw0x
Feb 23, 2016
Tobias Müller
Feb 23, 2016
rsw0x
Feb 23, 2016
Xinok
Feb 23, 2016
rsw0x
Feb 23, 2016
Xinok
Feb 24, 2016
Kagamin
Feb 25, 2016
Kagamin
Feb 25, 2016
Kagamin
Feb 26, 2016
crimaniak
Feb 20, 2016
Xinok
Feb 22, 2016
Ice Create Man
Feb 22, 2016
Yuxuan Shui
Feb 23, 2016
Russel Winder
February 18, 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
February 19, 2016
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
>
>
> 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

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
}

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
>
> 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.

-- 
/Jacob Carlborg
February 19, 2016
On Friday, 19 February 2016 at 12:16:49 UTC, Jacob Carlborg wrote:
> 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.
>

IMHO, without language support looks really bad - `map` really sticks out here.

What Rust is doing:

```
let foo: Option<i32> = bar();

let new_stuff = match foo {
   Some(x) => x,
   None => 0
}
```

(or similar, I don't have compiler handy).
February 19, 2016
On Friday, 19 February 2016 at 12:28:14 UTC, Nemanja Boric wrote:
> What Rust is doing:
>
> ```
> let foo: Option<i32> = bar();
>
> let new_stuff = match foo {
>    Some(x) => x,
>    None => 0
> }
> ```
>
> (or similar, I don't have compiler handy).

I think most would write that as:

    let new_stuf = bar().unwrap_or(0);

When you use the methods on `Option` you rarely need to match.
February 19, 2016
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.

February 19, 2016
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.

February 20, 2016
Yuxuan Shui <yshuiv7@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
February 20, 2016
On Saturday, 20 February 2016 at 09:40:40 UTC, Tobias Müller wrote:
> Yuxuan Shui <yshuiv7@gmail.com> wrote:
>>         [...]
>
> 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

D has this too, but only for nullable types afaik.

if(byte* ptr = someFunc()){
//...
}

February 20, 2016
On Friday, 19 February 2016 at 21:30:37 UTC, Yuxuan Shui wrote:
>> 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.
>

Swift also has guard, which solves this issue.

func ex(a: Int?) {
    // unwrap (possible to add additional check if desired)
    guard let a = a where a > 0 else {
        return // fail
    }

    a.ok // a is now a plain validated Int
}

February 20, 2016
On 2/20/16 5:04 AM, Daniel N wrote:
> On Friday, 19 February 2016 at 21:30:37 UTC, Yuxuan Shui wrote:
>>> 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.
>>
>
> Swift also has guard, which solves this issue.
>
> func ex(a: Int?) {
>      // unwrap (possible to add additional check if desired)
>      guard let a = a where a > 0 else {
>          return // fail
>      }
>
>      a.ok // a is now a plain validated Int
> }
>

I'm glad you pointed this out, I did not know this. Having to rename variables all the time is quite annoying.

-Steve
« First   ‹ Prev
1 2 3