News for hackers. Stuff that matters.
Re:Things Go needs more than generics by kitd on Monday October 04, 2021 @04:32AM (#28744416)
With generics, Optionals in Go become much more ergonomic.
Re:Things Go needs more than generics by tgv on Monday October 04, 2021 @05:12AM (#28744663)
But they add an unnecessary overhead, and still don't prevent you actually writing x := nil; print(x.a);. And everyone can have their own Optional, meaning you'll going to install dozens of different generics libraries for just this feature.

There are simple, go-like solutions. There's a character for pointer, so why not a character for a non-nil pointer?

Re:Things Go needs more than generics by kitd on Monday October 04, 2021 @06:13AM (#28744961)
And everyone can have their own Optional, meaning you'll going to install dozens of different generics libraries for just this feature.

Tbf, this is simply solved by putting a generic optional in the stdlib.

Re:Things Go needs more than generics by tgv on Monday October 04, 2021 @08:49AM (#28745993)
How would it work, BTW?

In Rust, you're forced to match against the enum before you can look at the value, but in Go you even can't match. So unless it's implemented as a type cast, testing optionality would be just like testing against nil. The current internal "optional" package just panics, which is just as useful as a panic on dereferencing nil.

Re:Things Go needs more than generics by kitd on Monday October 04, 2021 @10:27AM (#28747171)
Similar to a Java Optional<T> [1]

Give it an interface that allows you to attach behaviours for not null and null values.

[1] https://docs.oracle.com/en/java/javase/11/docs/api/java.base...

Re:Things Go needs more than generics by tgv on Tuesday October 05, 2021 @02:48PM (#28763831)
Function passing/callbacks, no thanks. You still won't get a compiler error when passing a potentially nil value, and I'd rather have readable code. I hope Optional is not going to be in the way of progress in this area.