News for hackers. Stuff that matters.
Re:From zero to Go: launching on the Google homepage in 24 h... by tibbe on Wednesday December 14, 2011 @03:29AM (#3351102)
Reading the same code I also cringed, but for a different reason: the validity of `paths` is tied to the validity of `err`; you should never look at at `paths` without first checking `err`. We can solve this much better using sum types (also known as discriminate unions):

    // Pseudo Go:
    maybe_paths := filepath.Glob(dir + "/*.png")
    case maybe_paths of {
      Some paths: {
        // Do things with paths
      }
      None: {
        // Handle error case
      }
    }
This makes it impossible to use `paths` if `Glob` returns an error, as `paths` won't be in scope!

Furthermore, not having sum types forces us to include a distinguish value `null` for reference (pointer) types, as a way to communicate "no value". This is bad as now we cannot distinguish references that should never be `null` from those than can be `null`.

Re:From zero to Go: launching on the Google homepage in 24 h... by merijnv on Wednesday December 14, 2011 @06:04AM (#3351403)
You can distinguish between nulls which are errors and nulls that are just the actual return value. That is what err is for.

The entire reason Go has multiple return values for functions (such as paths and err in this case) is to avoid having to encode errors in the "real" function result.

Now I agree, that this is less nice as say pattern matching and real sum types, but if you know how to appreciate those just switch to a functional language!

Re:From zero to Go: launching on the Google homepage in 24 h... by masklinn on Wednesday December 14, 2011 @07:04AM (#3351512)
> The entire reason Go has multiple return values for functions (such as paths and err in this case) is to avoid having to encode errors in the "real" function result.

That's nonsense, MRVs are tuples (conceptually, Go implemented them with special syntax because... well, it's Go so it could not go with the general principle now could it?), so Go very specifically encodes errors in the function result.

And using a sum type would encode the error in the type, not in the result itself.

Re:From zero to Go: launching on the Google homepage in 24 h... by 4ad on Wednesday December 14, 2011 @12:31PM (#3352690)
Multiple return values are not tuples, Go doesn't have tuples in the type system.
Re:From zero to Go: launching on the Google homepage in 24 h... by masklinn on Wednesday December 14, 2011 @04:56PM (#3353818)
> Multiple return values are not tuples

Multiple return values are a special case of tuples. Hence the "conceptually" part.

> Go doesn't have tuples in the type system.

Could you explain how you managed to completely miss the part where I say exactly that?

Re:From zero to Go: launching on the Google homepage in 24 h... by enneff on Wednesday December 14, 2011 @05:59AM (#3351394)
This kind of fussy type system is at odds with Go's design goals. It would make the language more abstracted from the underlying machine for little gain. I can't think of an instance of the issue you describe causing problems in real Go code.
Re:From zero to Go: launching on the Google homepage in 24 h... by masklinn on Wednesday December 14, 2011 @07:06AM (#3351521)
> This kind of fussy type system is at odds with Go's design goals.

There is nothing fussy about it, and the only way in which it's at odds with Go's design goals is that Go's design goals include things like "fuck you, only core types get to be generic", "that these mistakes were resolved 20 years ago does not mean we're not going to re-introduce them" and "if we give it a different name it's not the same thing, nah nah nah".

Re:From zero to Go: launching on the Google homepage in 24 h... by tibbe on Wednesday December 14, 2011 @07:11AM (#3351531)
The problem it introduces is that you could have gotten rid of nulls from the language if you use sum types for errors.