News for hackers. Stuff that matters.
Re:Grain: A strongly-typed functional programming language f... by masklinn on Tuesday July 31, 2018 @08:35AM (#17652630)
> This is something I wonder about JS: there are a lot of places that exceptions happen in (say) Python that just return `NaN` or `undefined` in javascript. Is it intentional? Is it a good idea?

Yes and no respectively.

It's intentional in the sense that the original Javascript was not intended to fault (much) because it was for basic scripting, so if one small script blew up it should not bring down the entire page's scripting. This also resulted in Javascript's exceptions facilities being very shitty (exception handling is not very flexible/convenient, and while that may have changed since — not sure — for a long time it was impossible to sub-type the native Error).

However it's not really a good idea for non-trivial software systems, it makes errors much harder to notice, recover from and debug. In fact "modern" JS APIs tend to fault e.g. parsing invalid JSON raises SyntaxError, it doesn't return null or undefined; and promises and async functions will convert exceptions to failures.

Re:Grain: A strongly-typed functional programming language f... by repsilat on Tuesday July 31, 2018 @04:08PM (#17656622)
Invalid JSON parsing as `null` would be an exciting API decision, haha.

One thing I think I like about JS over Python is that empty containers are "truthy". This, combined with non-raising missed lookups, tends to be pretty ergonomic. For me, at least.

Though it is messier -- in Python `key in d` is well entrenched as an idiom, and in JS `if(d.key)` misfires if the value is zero, and `if(d.key === undefined)` doesn't discriminate between a missed lookup and a stored `undefined`. Though the latter must be discouraged, right?

Re:Grain: A strongly-typed functional programming language f... by masklinn on Wednesday August 01, 2018 @04:10AM (#17660475)
> Invalid JSON parsing as `null` would be an exciting API decision, haha.

PHP's json_decode does that. And yes `null` is also returned as a PHP NULL.

> Though it is messier -- in Python `key in d` is well entrenched as an idiom, and in JS `if(d.key)` misfires if the value is zero

Or false, or null, or the empty string, or NaN.

FWIW `if (key in d)` also works in JS if d is a native object, though you need `d.has(key)` if d is an actual Map object instead.