News for hackers. Stuff that matters.
Re:Using the switch(true) pattern in JavaScript by Jtsummers on Monday April 12, 2021 @05:04PM (#26785169)
> Early return can get into trouble if you're talking about a function that has a fair bit of state to unwind in the event of an error. Open filehandles, allocated memory, general cleanup stuff.

This is where Lisp's unwind-protect, finally blocks in some languages, defer in Go, and C++'s RAII pattern can come in handy. Especially if you have a healthy respect for goto and want to minimize its presence in your code for various reasons.

Re:Using the switch(true) pattern in JavaScript by golergka on Monday April 12, 2021 @06:07PM (#26785774)
`finally` introduces additional blocks, so if you have 5 different resources that you acquire as your function goes along, you end up with 5 levels of curly braces. Not very elegant.

`defer`, on the other hand, is probably the only thing I wanted to take from Go to other languages I worked with. Beautifully explicit and wonderfully useful.

Re:Using the switch(true) pattern in JavaScript by shhsshs on Monday April 12, 2021 @07:23PM (#26786387)
C#’s new syntax for `using` also helps:

Old:

    using (var file = OpenFile())
    {
        // use file
    }
New:

    using var file = OpenFile();
    // file will be disposed when it goes out of scope
Re:Using the switch(true) pattern in JavaScript by earthboundkid on Monday April 12, 2021 @07:42PM (#26786520)
> `finally` introduces additional blocks

It would be cool if someone made a way to scope variables at the function level in JavaScript instead of at the block level. I might write a transpiler for it…

Re:Using the switch(true) pattern in JavaScript by golergka on Monday April 12, 2021 @08:53PM (#26787005)
I've never considered writing a proposal for a language feature before, but after 2 minutes of googling, this actually looks completely doable:

https://github.com/tc39/ecma262/blob/master/CONTRIBUTING.md

So, impostor syndrome aside, why not? Wanna team up on that?

Re:Using the switch(true) pattern in JavaScript by earthboundkid on Tuesday April 13, 2021 @12:04PM (#26794303)
whoosh.gif
Re:Using the switch(true) pattern in JavaScript by golergka on Tuesday April 13, 2021 @02:15PM (#26796648)
Okay, it was 2am here and I must have misread your comment and lost the 'variables' part.
Re:Using the switch(true) pattern in JavaScript by jakopo87 on Tuesday April 13, 2021 @03:20AM (#26789083)
It is already possible: var makes a variable function-scoped, let/const makes a variable block-scoped. Or are you referring to something else?
Re:Using the switch(true) pattern in JavaScript by throwanem on Monday April 12, 2021 @07:27PM (#26786412)
I don't know of an equivalent in the native API, but Bluebird's "disposer" implements this in the context of promises: http://bluebirdjs.com/docs/api/disposer.html

It would be nice if there were an equivalent in the native API. You can use .finally() with chained promises, but as far as I know there's nothing comparable with async/await yet, and that's a much more comfortable syntax with which to work with promises overall.