top | item 28897247

(no title)

drvd | 4 years ago

In Go

    func foo(x int) { return x + 1 }
    var y *int = nil
    foo(y) // compiler will flag thi

discuss

order

cy_hauser|4 years ago

That wasn't the greatest example as Go doesn't really have any way to represent it. If it did it might be something like this.

  type MyStruct struct {
    Name string
  }

  var-not-nil myStruct = &MyStruct{"Hello"} // this is a "not-nil" pointer variable

  myStruct = nil // compiler would catch this
The idea being to allow variables to hold and pass pointers to structs like now but ensure they are never nil.

Gwypaas|4 years ago

Now add an error to foo and have x=0 as a valid return value, or even worse, having to deal differently with different errors.