News for hackers. Stuff that matters.
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by biztos on Sunday September 04, 2016 @07:50PM (#12427294)
I too find it annoying in Go, though I'm not sure what the default value of a reference in a struct would be otherwise.

However, I do see the value of NULL in a database context even though it makes database interfaces harder -- especially in Go, where the standard marshaling paradigm means anything NULLable has to be a reference and thus have a nil-check every time it's used.

The conceptual match is so awkward that when I write anything database-related for Go, if I have the option then at the same time I make everything in the database NOT NULL; even though that screws with the database.

Ah, NULL. When I think about the pain it causes, balanced against its utility, I sometimes wish I'd never heard of it.

And I'm sure learned people said the same thing about Zero, once upon a time.

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by unscaled on Sunday September 04, 2016 @10:07PM (#12427696)
Go's wholesale embrace of null is kinda jarring at this day and age, since it doesn't even have safe null dereferencing operators like Groovy, C#, Kotlin et. al. It's like Java all over again.

Considering Rob Pike is a huge fan of Tony Hoare, and the inevitable mountain of pain caused by null references, that's kinda surprising.

But I guess "Worse is Better" in the sense of "simplicity of implementation is valued over all else" is still the guiding principle of Go. As Tony Hoare himself said: "But I couldn't resist the temptation to put in a null reference, simply because it was so easy to implement". It seems like we're doomed to repeating this mistake again and again.

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by simcop2387 on Sunday September 04, 2016 @09:57PM (#12427666)
> I too find it annoying in Go, though I'm not sure what the default value of a reference in a struct would be otherwise.

The way it works in Rust is that you can't have a reference without the thing you're referring to. There isn't a default value and because of that it's an error to try to have a reference without something to refer to.

The way Rust works with this is to have a type called Option (I think this is a monad in haskell?) that lets you say this can be None or Some, so you have to explicitly handle the None case whenever you use it (either by panicing, or matching or some other method).

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by gbacon on Sunday September 04, 2016 @10:12PM (#12427715)
You’re thinking of Maybe[1]:

  data Maybe a = Just a | Nothing
      deriving (Eq, Ord)
Yes, it is a monad[2].

  return :: a -> Maybe a
  return x  = Just x

  (>>=)  :: Maybe a -> (a -> Maybe b) -> Maybe b
  (>>=) m g = case m of
                 Nothing -> Nothing
                 Just x  -> g x
[1]: https://wiki.haskell.org/Maybe

[2]: https://en.wikibooks.org/wiki/Haskell/Understanding_monads/M...

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by aweinstock on Sunday September 04, 2016 @11:40PM (#12427951)
It's a monad in Rust too (with Some being return, and and_then[1] being >>=). Rust's generics just aren't yet[2] flexible enough to abstract over monads within the language.

[1] https://doc.rust-lang.org/std/option/enum.Option.html#method... [2] https://github.com/rust-lang/rfcs/issues/324

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by IanCal on Tuesday September 06, 2016 @04:06AM (#12434328)
For anyone who has the same reaction as I did when first hearing of this, "But isn't that just like a nullable value?" (don't worry, this post will not contain any monad analogies)

Yes. Ish. In languages that have a null/nil/None then almost anything you have passed to any function could be null. Every function you write could have a null passed into it and you either have to do checks on everything or trust that other people will never pass those values in.

That's pretty OK until something unexpectedly receives one and hands it off to another function that doesn't deal with it gracefully.

In Haskell (and I assume others) this can only happen to Maybe types, they're the only ones that can have the value of Nothing. So the compiler knows which things can and cannot be Nothing, and therefore can throw an error on compilation if you are trying to pass a Maybe String (something that can be either a "Just String" that you can get a String from or a Nothing) into a function which only understands how to process a String.

This feels like it might be restrictive, but there are generic functions you can use to take functions which only understand how to process a String and turn it into one that handles a Maybe.

It's quite a nice system, even though I get flashbacks to java complaining I haven't dealt with all the exceptions.

Haskell doesn't completely stop you from shooting yourself in the foot though, you can still ask for an item from an empty list and break things. There are interesting approaches to problems like that however: http://goto.ucsd.edu/~rjhala/liquid/haskell/blog/about/

Finally, it's worth pointing out that Maybe and Nothing and Just and all that aren't built into the language, they're defined using the code that gbacon wrote. So in a way, Haskell doesn't have a Maybe type, people have written types that work really well for this kind of problem and everyone uses it because it's so useful.

[disclaimer: I've probably written 'function', 'type', 'value' and other terms with quite specific meanings in a very general way. Apologies if this hurts the understanding, and I would appreciate corrections if that's the case, but just assume I'm not being too precise if things don't make sense]

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by simcop2387 on Tuesday September 06, 2016 @04:42PM (#12439150)
> Finally, it's worth pointing out that Maybe and Nothing and Just and all that aren't built into the language, they're defined using the code that gbacon wrote. So in a way, Haskell doesn't have a Maybe type, people have written types that work really well for this kind of problem and everyone uses it because it's so useful.

Yep, and I believe it's the same with with Rust, it's been put into the standard library but it's not some special thing that only the compiler can make.

And like you said, the whole idea is that the normal case is that you can't have null values. If you need them for something you declare that need explicitly and have to handle it explicitly or it's a compile error. That way it can be statically checked that you've handled things.

Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by merijnv on Monday September 05, 2016 @05:19AM (#12429084)
Option (and/or option) is the usual name of Maybe in ML style languages like SML, ocaml, etc.
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by TylerE on Sunday September 04, 2016 @09:31PM (#12427594)
What's wrong with using an Option/Maybe type? Can't Go do that?
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by cyphar on Sunday September 04, 2016 @09:45PM (#12427627)
You could (just use an interface). However, it would be a pain to use because Go doesn't have generics (if a type X implements the Maybe interface, it is not true that []X can be casted to []Maybe). So you would have to always make temporary slices to copy all of your Xs into.
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by brobinson on Sunday September 04, 2016 @09:56PM (#12427660)
Go effectively has Option types for database query results: https://golang.org/pkg/database/sql/#NullBool
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by unscaled on Sunday September 04, 2016 @10:15PM (#12427724)
This is not a generic option type, but rather a tri-state bool, or an Option<bool>. Go has no user-defined generics, so you can't have a bool type. It does have built-in "magical" generics, namely arrays, slices maps and channels, but no option/maybes. Language-level option types are not unheard of (C#, Swift and Kotlin all have a notion of this sort, although they all support proper user-defined generics as well).
Re:Hello, I’m Mr. Null. My Name Makes Me Invisible to Computers by masklinn on Monday September 05, 2016 @05:54AM (#12429211)
> Language-level option types are not unheard of (C#, Swift and Kotlin all have a notion of this sort)

Swift's Optional is a library type: https://github.com/apple/swift/blob/master/stdlib/public/cor...

Though the compiler is aware of it and it does have language-level support (nil, !, ?, if let)