by knocte · 7mo ago · 0 comments
> Go was not satisfied with one billion dollar mistake, so they decided to have two flavors of NULL
Thanks for raising this kind of things in such a comprehensible way.
Now what I don't understand is that TypeScript, even if it was something to make JavaScript more bearable, didn't fix this! TS is even worse in this regard. And yet no one seems to care in the NodeJS ecosystem.
<selfPromotion>That's why I created my own Option type package in NPM in case it's useful for anyone: https://www.npmjs.com/package/fp-sdk </selfPromotion>
function fnO(val?: number) {
if (val == null) {
return "NAH";
} else {
return (val * val).toString();
}
}
test("testing Options", () => {
let foo = undefined;
let bar = 2;
expect(fnO(foo)).toBe("NAH");
expect(fnO(bar)).toBe("4");
});
Your readme states> FP's languages approach of rather not having null at all
But `None` is just another null / undefined, which brings along a bunch of non-idiomatic code around handling it.
If you do a type check with None, and there is some value inside (so it is Some, not None), it is IMPOSSIBLE that the .value that you extract underneath is gone. This is an important race-condition that you might run into due to the nature of TS/JS, but by boxing the value with an immutable Option type, you're protected.
Also this prevents people to run into NullReferenceException (or UndefinedRefsExceptions, or whatever is called in this ecosystem) for people that didn't turn strictNullChecks ON.
But yeah it's a fair point. Sometimes I think I should just write my own lang (a subset of typescript), in the same fashion that Lisette dev has done.
You can't enforce it in any normal codebase because null is used extensively in the third party libraries you'll have to use for most projects.