3 ms·
I really wish python would steal 'let' from JS, specifically in a block scoped form - coming from perl's 'my' as a way of life, python's (and ruby's) function l
by mst 3y ago
I really wish python would steal 'let' from JS, specifically in a block scoped form - coming from perl's 'my' as a way of life, python's (and ruby's) function level scoping and "surprise! variable just popped into existence on assignment!" behaviour really messed me up.
- boppo1 3y agoCan you tell me more about the usefulness of 'let'? I'm an intermediate(?) programmer but I've worked mostly in Python, C and C++. I did one JS project and everything I saw about 'let' seemed redundant.
- mst 3y agoFirstly (compared to python at least) there's the obvious 'typo in variable name barfs early' advantage, which honestly is not to be sniffed at on its own. But my big thing is about being able to scope values to the specific block where they belong: ... let foo; { let x = blah(); let y = frob(x); foo = munge(y); } and the temporaries 'x' and 'y' have gone away as soon as the } happens. also, if you've ever had "fun" trying to capture a loop variable, for (let val of things) { callbacks.push(() => val.finish()); } has a different 'val' for each iteration of the loop so you can do that safely without requiring some sort of copying or an intermediate function invocation to create an extra scope. Oh, and it also renders impossible certain weird constructs like if <test>: some_name = ... where now whether the some_name variable even -exists- after that if block is indeterminate without knowing which way the <test> went. I also like being able to do e.g. for (let thing of things) { let blah = thing.blah; callbacks.push(() => blah.doThing()); } Without 'let' you'd have to write (for the first 'for' example) for (var val of things) { callbacks.push(((val) => { return () => { val.finish() } })(val)); } to perpetrate let-over-lambda to get a separate scope for the callback function. This is not my favourite thing to write, and -very- far from my favourite thing to read later. Hopefully that gives you a better idea of why I, at least, prefer having 'let' available.