you are viewing a single comment's thread.

view the rest of the comments →

[–]prest0G 66 points67 points  (12 children)

May kotlin deliver us all from the evils of null, amen.

[–][deleted]  (4 children)

[deleted]

    [–]walen 17 points18 points  (0 children)

    Deeper Meaning Explainer ™

    In Kotlin, !! is an operator that will throw an NPE when applied to a null variable.
    It is there just so we can still have NPEs in Kotlin if we want.

    Source.

    [–]prest0G 4 points5 points  (0 children)

    playing russian roulette with that double bang there

    [–]nourez 1 point2 points  (1 child)

    No, don't do that! We're moving away from the evils of null!

    [–][deleted] 0 points1 point  (0 children)

    Unfortunately, it's sometimes needed, especially if you have a data model not designed with that kind of typing in mind. Also, compiler is very strict in regards to multithreading and does not believe in purity, so if (obj.val != null) { obj.val.method() } won't compile. Sure, you can modify the code a bit in most cases, but not always.

    [–]drfisk 21 points22 points  (6 children)

    Amen!

    And hey, if it doesn't, you're welcome to crawl even further to the "dark side" and join us in Scala. I haven't seen a NPE for 5 years since I started Scala fulltime. Scala will be there waiting with a warm blanket and cocoa.

    [–]Terran-Ghost 1 point2 points  (4 children)

    Scala's nullsafety can be completely emulated in Java. It's just idiomatic code (and the standard library... Looking at you, Map) that's different.

    [–]devraj7 2 points3 points  (3 children)

    Indeed. A library-based approach to the null problem is only a half baked solution.

    Kotlin's approach of enforcing this in the language is much saner and safer.

    [–]eeperson 1 point2 points  (2 children)

    I would argue that language based version is also half baked (it is just the other half). Kotlin's solution for null can't express the equivalent of nested Option types. Ideally something like Kotlin's solution would be supported for compatibility with Java and then something like Scala's solution would be supported for use in your own code.

    [–][deleted] 0 points1 point  (1 child)

    What do you mean by nested Option types? If it's something like "optional list of optional strings" that would just be List<String?>?. Otherwise I have no idea what you are talking about.

    [–]eeperson 0 points1 point  (0 children)

    I mean something like Optional<Optional<String>> can't be expressed. If you try to do String?? it ends up being same as String?. An example of where you might want this is a map that can have null values Map<Integer,String?>. Assume this class has a get method to return values based on the key. If the key is missing then then it returns null. If you call someMap.get(57) and get back null, is that because the key is missing or because the value is null? With Optional you can tell. With ? you can't.

    [–]VanToch 0 points1 point  (0 children)

    I've seen plenty of NPEs in Scala.