News for hackers. Stuff that matters.
Re:Things Go needs more than generics by brundolf on Monday October 04, 2021 @01:57AM (#28743522)
> It’s crazy how Rust just gets rid of this whole class of bugs

As do TypeScript, and Kotlin, and Dart, and Python (with MyPy), and C#, and Swift...

It's honestly inexcusable that Go, as a relatively new statically-typed language, doesn't have strict null-checks

Re:Things Go needs more than generics by mbStavola on Monday October 04, 2021 @05:13AM (#28744667)
I don't know about C# and Swift, but Kotlin, Python, and Typescript are only null-safe-ish; there is always a relatively easy path to smuggling a null value into your program regardless of the guarantees of the types you use.

This isn't to say it's useless, just that it's not as useful as it _could_ be. I've seen codebases in all three languages where there have been real production issues due to null values, even though the type decls say otherwise. Kotlin probably does the best job here but using any JVM library introduces potential (K)NPEs.

Re:Things Go needs more than generics by unscaled on Monday October 04, 2021 @07:16AM (#28745281)
This situation is unfortunately quite hard to avoid in the languages above since they either added null safety (or indeed any kind of type-checking) at a later stage (C# and Python) or they need to maintain compatibility with a language or platform that is not null-safe (Kotlin with Java, Swift with Objective C, TypeScript with JavaScript).

Languages like Rust, Haskell or ML do not have this issue since they do not need to support such a legacy layer.

Re:Things Go needs more than generics by pjmlp on Monday October 04, 2021 @07:45AM (#28745474)
Kind of true, there is still the possibility to smuggle null values via unsafe, or FFI, but then the fault is on whoever certified the code as safe.
Re:Things Go needs more than generics by unscaled on Monday October 04, 2021 @09:29PM (#28754505)
I was hesitating whether to mention unsafe code blocks or FFI, but the no language that gets thing done can avoid abuse of its escape hatches. Even Haskell has to deal with nulls when doing FFI.
Re:Things Go needs more than generics by brundolf on Monday October 04, 2021 @11:12AM (#28747728)
There's still a huge difference between "you can smuggle a null value in" and "the language can't statically reason about null values at all"

You can override TypeScript's checks for anything, not just nulls, and yet people still get tons of benefit from it. Same goes with many of Rust's safety checks in unsafe { } blocks.

Additionally, languages like C# and typescript have to be a little more flexible because they added this feature after the fact. Go had plenty of opportunity to do it up front and not require any compromises, and it chose not to.

Re:Things Go needs more than generics by wildbook on Monday October 04, 2021 @06:02AM (#28744902)
As much as I like C#, your points apply to it too.
Re:Things Go needs more than generics by mtelgon on Monday October 04, 2021 @10:01AM (#28746800)
can you give an example of where typescript fails to protect you against nulls in your own code? (apart from the use of any
Re:Things Go needs more than generics by brundolf on Monday October 04, 2021 @11:07AM (#28747663)
You can turn off strictNullChecks, and there's also noUncheckedIndexAccess which unfortunately isn't enabled by default even in strict mode. You can also introduce flaws via @ts-ignore, casting, etc.

Still, typescript equips you to catch these errors, even if you can technically circumvent it. In practice it can be nearly bullet-proof if you follow good practices.

Re:Things Go needs more than generics by mbStavola on Monday October 04, 2021 @07:11PM (#28753551)
Aside from explicitly turning off null safety and tricky use of casting, an easy example is interfacing with JS.

If you're using either a library that wasn't written in pure TS (maybe JS or JS with .d.ts) or interacting with some unconverted JS from your own codebase, you can easily pass a null through entirely by accident. The problem really stems from the JS end of things, but 9 times out of 10 you're going to be touching JS at _some_ level when using TS so I think it's fair to point out this gap.

Re:Things Go needs more than generics by appletrotter on Monday October 04, 2021 @11:12AM (#28747735)
Abusable things:

- !

- as x

- @ts-ignore

Re:Things Go needs more than generics by skohan on Monday October 04, 2021 @07:10AM (#28745240)
How can you get an unchecked nil into a swift program without the unsafe API?
Re:Things Go needs more than generics by bobbylarrybobby on Monday October 04, 2021 @10:10AM (#28746931)
Implicitly unwrapped optionals
Re:Things Go needs more than generics by skohan on Monday October 04, 2021 @04:48PM (#28751890)
But this is exactly equivalent to calling `.unwrap()` in rust, no?
Re:Things Go needs more than generics by virgilp on Monday October 04, 2021 @06:06AM (#28744926)
C# only sort-of-fixed this problem and it did so rather inelegantly IMO.

E.g. nullable types are not options - you can't "map" them (you can't invoke Select, Where etc). It's easy to "get value or default" (via operator `??`), but you can't do the other, equally frequent thing, of "apply transformation on value if not-null". Or... well, you can, but here's the syntax:

    var newVal = val == null ? (resultType?) null : compute(val.Value);

instead of

    var newVal = val.Select(compute);
Re:Things Go needs more than generics by mathw on Monday October 04, 2021 @06:17AM (#28744978)
Various bits of C# and its libraries also variously ignore non-nullability when it suits them. I know it's done the way it's done for compatibility reasons, but it's really quite confusing around the edges as a result. And despite something being non-nullable in the code it could still be null at runtime thanks in my experience particularly to the joys of deserialisation libraries.
Re:Things Go needs more than generics by CodesInChaos on Monday October 04, 2021 @07:51AM (#28745516)
For that I use my own extension function like `?.Map(x => ...)`, defined as `Map(this T input, Func<T, TResult> map) => map(input)`. Still a bit ugly, but much nicer than the ternary operator.

Of course that doesn't fix all the other issues with null handling in C# (inconsistency between null references and null value types, inability to nest options, ...)

Re:Things Go needs more than generics by andy_ppp on Monday October 04, 2021 @05:47AM (#28744843)
As someone working on a Typescript codebase with "string? | undefined" everywhere I wish this were true.
Re:Things Go needs more than generics by tticvs on Monday October 04, 2021 @04:25AM (#28744371)
Unfortunately it's easy enough to confuse MyPy that you can't really fit in with the rest of this list.
Re:Things Go needs more than generics by rowanG077 on Monday October 04, 2021 @04:50AM (#28744532)
Unfortunately in any real world typescript or python the type checking is almost always partial. Which means it doesn't actually save you from these kind of errors.
Re:Things Go needs more than generics by cageface on Monday October 04, 2021 @05:07AM (#28744634)
I have not found this to be the case in Typescript code I've worked on. The main issue I've found with TS is stuff coming in from outside the system as JSON that doesn't conform to expected formats.
Re:Things Go needs more than generics by jakelazaroff on Monday October 04, 2021 @07:02AM (#28745200)
You can solve the JSON issue by validating that it deserializes to a known type at the point of ingress. I wrote a small library that I use in basically every project to help with that: https://www.npmjs.com/package/narrows
Re:Things Go needs more than generics by CryZe on Monday October 04, 2021 @05:24AM (#28744717)
One issue I've noticed is that indexing into a T[] array where T can't be undefined, results in the expression to be of type T, not T | undefined, despite an out of bounds index evaluating to undefined. I believe I've seen that they recently added a setting that changes this though.
Re:Things Go needs more than generics by cageface on Monday October 04, 2021 @05:39AM (#28744790)
A lot of statically typed languages leave array indexing as a loophole for convenience but yeah this can also be a source of errors.
Re:Things Go needs more than generics by andy_ppp on Monday October 04, 2021 @05:48AM (#28744845)
Consider using https://github.com/gcanti/io-ts to help with that...