News for hackers. Stuff that matters.
Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by meddlepal on Monday May 30, 2016 @12:11AM (#11799506)
The problem isn't null itself, but that many languages use nullable types by default.

Null is a damn useful construct and while there may be better solutions to using a null reference they're often not as clear as using null or worth the time investment to develop properly.

So yea, I generally try to avoid null's and completely understand how they've caused a lot of problems, but I'm not willing to say we shouldn't have them or use them when appropriate.

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by kazagistar on Monday May 30, 2016 @02:51AM (#11799947)
You don't just not use nulls, you replace them with something like Optionals. With a bit of language support, they can compile down to nulls but provide a layer of type safety and convenience functions.
Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by kmiroslav on Monday May 30, 2016 @02:53AM (#11799952)
It's perfectly fine to use nulls in languages that encode them in their type system, e.g. Kotlin.
Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by junke on Monday May 30, 2016 @03:11AM (#11800017)
Exactly. The problem is not that there is a null, it is that it is a valid value for all types (e.g. you can pass null to a function that expects a String).

Allowing nulls explicitly, like with a "String?" type, is the way to go in languages where Option is not an option.

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by kqr on Monday May 30, 2016 @05:53AM (#11800523)
Isn't it, in practise, basically the same thing though? The theoretical difference is this:

1. Option is a regular data type/object that represents "a collection of at most one value". It's similar to a list, set, heap, tree or whatever other collection types you have, except it can not contain more than one value.

2. With explicit nulls I guess any data type (e.g. Integer) will automatically get a clone of itself (a different type named Integer?) where also null is a valid member.

From a purely theoretical standpoint, I like #1 better.

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by greydius on Monday May 30, 2016 @07:14AM (#11800733)
In practice, it's not the same thing.

Here's a Java example where I want the type system to enforce that a method in `ClassB` can only be called from `ClassA`. However, the fact that `null` circumvents the type system makes this pattern just wishful thinking.

    class ClassA {
        private static final Witness witness = new Witness();

        final static class Witness {
            private Witness() {}
        }

        void callClassBMethod(ClassB classB) {
            classB.onlyClassACanCallThisMethod(witness);
        }
    }

    class ClassB {
        void onlyClassACanCallThisMethod(ClassA.Witness witness) {
            // ...
        }
    }
Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by SatvikBeri on Monday May 30, 2016 @11:16AM (#11801679)
I think Option[String] and "String?" are semantically both a union type–they both effectively add one possible value to the underlying type, along with a constructor for naturally extending functions on that type–Kotlin's "?" operator is equivalent to Scala's map or Haskell's fmap, etc.
Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by airless_bar on Monday May 30, 2016 @01:06PM (#11802145)

  Option[Option[String]] != String??
Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by SatvikBeri on Monday May 30, 2016 @03:42PM (#11802749)
My impression, though I'm not sure, is that Kotlin simply won't allow you to do a double "??". Scala etc. will technically allow Option[Option[Something]], but in practice you would almost never want to use it, and can easily avoid it with flatMap.
Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by airless_bar on Monday May 30, 2016 @05:36PM (#11803358)
The whole point of this was to show that Scala's types preserve the structure of the computation.

It might not be very interesting in the Option[Option[String]] case but imagine Try[Either[String, Int]] or List[Future[Double]].

It's a very important distinction.

Collapsing cases is one of the primary thing why exceptions sometimes get a bad rap, and Kotlin (and Ceylon) do the same with ? (and |, &) at the value level.

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by junke on Monday May 30, 2016 @02:30PM (#11802450)
Option[Option[String]]? Is this a Church Numeral?
Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by kmiroslav on Monday May 30, 2016 @10:44AM (#11801552)
The downside of `Option` is that it's a wrapper, and as such, you need to `flatMap` (or similar) whenever you want to access the wrapped value.

By encoding `null` in its type system, Kotlin lets you manipulate these values directly which leads to code that is much less noisy and just as safe.

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by airless_bar on Monday May 30, 2016 @01:18PM (#11802190)
Not really.

The main strength of the first approach is that Option is only one type out of many error-handling structures.

Not every error is handled appropriately by Option/?.

If you have a language like Kotlin where they hard-coded one way of handling errors, it feels very unidiomatic to pick a better fitting error handling type, while in languages where errors are handled by library code, it's a very natural approach.

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by kmiroslav on Monday May 30, 2016 @04:58PM (#11803155)
> Not every error is handled appropriately by Option/?.

Which is expected since these two constructs are not aimed at handling errors: they manage missing values.

> If you have a language like Kotlin where they hard-coded one way of handling errors

No, no. `?` is not for handling errors.

Kotlin is as agnostic as Scala for managing errors: you are free to use exceptions, dumb return values or smarter ones (`Either`, `\/`, `Try`, ...).

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by airless_bar on Monday May 30, 2016 @05:28PM (#11803311)
Yeah, it's just that-if you look at every language ever designed-if the language ships with a built-in construct developers will use and abuse it on every occasion, and every other approach lingers in obscurity.

> Which is expected since these two constructs are not aimed at handling errors: they manage missing values.

Which is a very small part of handling errors in general. As Kotlin offers special syntax for only this case, developers tend to shoehorn many errors into the "missing-value" design to get the "nice" syntax even if a different approach would have been more appropriate.

> Kotlin is as agnostic as Scala for managing errors: you are free to use exceptions, dumb return values or smarter ones (`Either`, `\/`, `Try`, ...).

That's not true in practice:

Just have a look at funktionale: Despite providing almost the same as Scala's error handling types (partially due to the blatant copyright violations) almost nobody uses it. This is a direct result from having a "first-class" construct in the language: It turns library-based designs into second-class citizens.

That's the thing Scala got right, and many of the copy-cat languages got wrong.

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by kmiroslav on Monday May 30, 2016 @05:46PM (#11803401)
> Which is a very small part of handling errors in general

Missing values are not errors.

If you look up a key on a map and that key is not present, it's not an error.

> partially due to the blatant copyright violations

Uh copyright what? On an API?!?

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by airless_bar on Monday May 30, 2016 @05:54PM (#11803447)
> Missing values are not errors.

Call it whatever you want. ? only covers a small subset of interesting "conditions" while tremendously hurting "conditions" which could be handled in a better way.

> Uh copyright what? On an API?!?

Implementation. The copying of slightly buggy exceptions strings makes it even more obvious that files were copied verbatim with just enough syntax changes to turn Scala code into Kotlin code while replacing the original license and authors with different ones.

PS: Feel free to comment on actual the points I made.

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by kmiroslav on Monday May 30, 2016 @11:12PM (#11804322)
> PS: Feel free to comment on actual the points I made.

Sure.

I think the idea that API's (or implementations as you said) can be copyrighted is completely insane and I can't believe any software engineer would be okay with it. Which makes me think you're not a software engineer, and that's okay, but please read up on the issues, this is super important for our profession.

I can't belive the US made that a law and it makes me sure that I will never want to move there.

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by airless_bar on Tuesday May 31, 2016 @01:26AM (#11804785)
> I think the idea that API's (or implementations as you said) can be copyrighted is completely insane and I can't believe any software engineer would be okay with it. Which makes me think you're not a software engineer, and that's okay, but please read up on the issues, this is super important for our profession.

I think you are super confused here. This is not about APIs. Copyright is what allows software developers to enforce a license of their choice. Without copyright, the license is just a text file without meaning. I suggest you read up on the FSF's position on this if you want to have an example.

> Sure.

(Still waiting for you to comment on the points I have made.)

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by pjmlp on Monday May 30, 2016 @07:49AM (#11800831)
Eiffel was one of the very first ones to have that feature actually, around 2006 when the language was revised for the ECMA standard.

https://www.eiffel.org/doc/eiffelstudio/Differences%20betwee...

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by legulere on Monday May 30, 2016 @05:56AM (#11800534)
This is e.g. what rust does. Another advantage of this is that you can also use them with types that aren't pointers (e.g. chars, integers, enums).
Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by yxlx on Monday May 30, 2016 @08:30AM (#11800957)
I would like to read about this. Got any links?
Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by legulere on Monday May 30, 2016 @09:07AM (#11801112)
Option<T> is just a normal enum in Rust. Enums can also can have parameters in Rust. Those are usually called tagged union in non-rust-speak. It's either Some(x) or None. It has a pretty easy definition: https://doc.rust-lang.org/std/option/enum.Option.html

Storing None for an Option<&T> as a null and Some(ref) just as the reference is an special optimisation that the rust compiler does. Usually you have a selector which tells you which variant of the enum it is.

Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by 0xfaded on Monday May 30, 2016 @12:40PM (#11802041)

  Tree *a = new Leaf();
  Tree *b = new Leaf();
  Tree *c = new Node(a, b);
  a.parent = b.parent = c;
null has other uses, e.g. as above for cyclically dependent initialisation.
Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by kazagistar on Wednesday June 01, 2016 @12:50PM (#11815976)
First, to some extent, your code is more a demonstration of the weakness of imperative vs declarative code, since, for example, Haskell lets you declare recursive structures directly, since it doesn't enforce an order of execution where none is needed.

    data Tree = Leaf Tree | Node Tree Tree
    a = Leaf c
    b = Leaf c
    c = Node a b
But more importantly, there is no reason you can't do this with Optionals.

    Tree *a = new Leaf();
    Tree *b = new Leaf();
    Tree *c = new Node(a, b);
    a.parent = b.parent = new Some(c);
Re:Null References: The Billion Dollar Mistake – Tony Hoare ... by bogomipz on Monday May 30, 2016 @11:16AM (#11801677)
On a related note and at the risk of sounding obtuse here - if you didn't have "null" wouldn't you just have to replace it with something else?