3 ms·
The most important thing about Kotlin is a bit hard to find — it’s licensed under the GPL-compatible Apache License 2.0: https://github.com/JetBrains/kotlin/blo
by kragen 11y ago
The most important thing about Kotlin is a bit hard to find — it’s licensed under the GPL-compatible Apache License 2.0: https://github.com/JetBrains/kotlin/blob/master/license/LICENSE.txt https://github.com/JetBrains/kotlin/blob/master/license/LICE...
Now that I know that, Kotlin looks pretty cool! It proposes solutions to a lot of the most painful problems in Java.
I'm not sure the solutions are always correct, though. When I hear "null safety", I think "pattern matching on a Maybe sum type to statically ensure non-nullness", but what the talk says Kotlin actually provides is pressuring you into writing a bunch of your code in the Maybe monad, thus spreading nullness far and wide. The talk example is "println(a?.length())".
Maybe that's the best pragmatic solution in Kotlin, or maybe the talk oversimplifies what Kotlin actually gives you, but it kind of seems like the wrong direction to go in to me (most of the time!)
- non-alexandro 11y agowhat about a?.let { println(it.length()) } or val let: Int? = arg?.let { it.length() } ?
- rattray 11y agoI don't think that's the way to do it in Kotlin (though I'm no expert). More idiomatic: fun myFunc(a: Int?): { if ( a == null ) { // handle the null case } // a is guaranteed non-null here } EDIT: And, of course, you can just use non-nullable types: fun myFunc(a: Int) { // a is guaranteed non-null }
- ufmace 11y agoI think the point isn't the Maybe stuff - Int? - but the non-nullable reference types. In conventional Java, every reference type is always nullable, so there's no way to strictly differentiate between things that couldn't possibly be null, and so don't need checking, and things that have a valid reason for being null, and should be checked and handled properly. Kotlin allows non-nullable reference types, so you can declare a method as taking a non-nullable reference to an object. Now within that method, you know that reference is never null, and don't need to check it. When you call it, you have to either check for null beforehand, or pass something that is already guaranteed not to be null. It's kind of like it builds a wall between things that could be null, and things that can't be, instead of having theoretical potential nulls all over your code. When you think about it, it seems like a pretty natural thing for a strongly-typed language to do.
- kragen 11y agoI agree that non-nullable reference types are a big advantage. (People who haven't programmed in Java might underestimate how big a problem null pointers are in Java.)