logoalt Hacker News

flowerthoughtstoday at 7:24 AM3 repliesview on HN

In this case, the missing piece in Go is the NonNullable hint. That would make it clear that null checks aren't needed, enforceable by the type system, and lintable.

Option types just forces you to do the check, but doesn't remove the need for it.

Now that we have generic types, a NonNullable intrinsic type seems doable...


Replies

ThePhysicisttoday at 8:51 AM

It's quite easy to write a generic Maybe struct that performs most of the encapsulation that Rust's Maybe does i.e. allow unwrapping of the inner type through a function or handling the nil case through a switch like statement. I've never seen this in the wild which makes me think people don't care about it too much. And of course it's runtime based so no compile time guarantees, and just to preempt the expected replies I know it's not the same what Rust is capable off and Rust is of course a much much much much better language than Go.

Personally I do experiment with these things as it makes code more readable, it just seems adoption for generics and what you can do with them is still quite low in the broader community. That said I do not deal with null pointer exceptions much at all, and when I do it's often relatively simply to spot and fix, so for me it's not a large issue.

cubefoxtoday at 7:41 AM

Or a set theoretic type system with union type declarations (foo|null), like in TypeScript.

sail0rm00ntoday at 7:25 AM

References like C++, maybe?