3 ms·
I looked for it in the article but I didn't see see it anywhere: In go you can have methods that execute with nil receivers. In practice this makes nil in go ve
by kjeetgill 8y ago
I looked for it in the article but I didn't see see it anywhere: In go you can have methods that execute with nil receivers. In practice this makes nil in go very different than null I'm java.
func main() {
var a A
a.getB().getM()
}
type A struct { B B; }
func (a A) getB() B {
log.Println("getB")
if a != nil { return a.B; }
return nil
}
type B struct { M string; }
func (b *B) getM() string {
log.Println("getM")
if b != nil { return b.M; }
return ""
}
Everything is nil but they both got executed! Obviously, you should use this selectively, but it's there!
Algebraic Data Types always seemed more elegant when I've seen them in other languages, anyone have anything but positive things to say about them?
- calebh 8y agoIsn't this just the Maybe monad without any benefits of abstraction?