News for hackers. Stuff that matters.
Re:Show HN: Returnif – the conditional return statement by Fudgel on Sunday July 29, 2018 @11:46PM (#17641901)
Could you perhaps use https://www.sweetjs.org/ instead of modifying babel?
Re:Show HN: Returnif – the conditional return statement by notheguyouthink on Monday July 23, 2018 @03:12PM (#17595086)
What's the `err` being returned here? I didn't think JavaScript was big on error returning, as might typically be used by a `returnif` keyword.

This, funny enough, reminds me of Go. We've been having debates about how to improve syntax of `if err != nil { return err }`, and many of us (myself included) disliked helpers to immediately return or hide an error value.

The reason is that `if err != nil { return err }` is actually somewhat non-idiomatic. It returns the error value with no additional context of where the error occurred, what is wrong, etc. So something like `returnif` in Go, I think is bad. For reference, this is a more idiomatic example: `if err != nil { return fmt.Errorf("foo bar: %v",err) }` where `foo bar` adds context about the thing returning the error.

So perhaps my Go logic doesn't apply here in JS land, but it seems like a bad keyword, if it's similar to Go.

Re:Show HN: Returnif – the conditional return statement by andynator1000 on Monday July 23, 2018 @05:00PM (#17595952)
The proposed syntax allows for a precondition which can evaluate an expression and return that.

So if you wanted to format the error returned you could do `returnif error : formatError("Error context:", error)`

Re:Show HN: Returnif – the conditional return statement by stockkid on Wednesday July 25, 2018 @06:01AM (#17608101)
Very interesting. However I'm not sure the benefit of saving couple of lines outweighs the cost of depending on a non-standard syntax thorough a transpiler.

You mentioned that this is a proof of concept hack. What's the plan ahead?

Re:Show HN: Returnif – the conditional return statement by adamlouis on Wednesday July 25, 2018 @02:13PM (#17611439)
Thanks! Agreed on the cost/benefit.

Was mostly a fun project to prove out & build a new idea. No plan ahead for this, but always new projects in the pipeline!

Re:Show HN: Returnif – the conditional return statement by gitgud on Wednesday July 25, 2018 @10:43AM (#17609617)
Cool concept! Looks much more readable in this context:

    err = checkErr()
    if (err)
        return err
Vs

    returnif checkErr()
However, seems a bit overboard to add a reserved keyword for this 'exceptional' case.
Re:Show HN: Returnif – the conditional return statement by adamlouis on Wednesday July 25, 2018 @02:24PM (#17611551)
Thank you! Definitely a bit overboard.

`returnif` is fun, but there's certainly value in the simplicity of restricted, opinionated syntax (e.g Go).

Re:Show HN: Returnif – the conditional return statement by caspervonb on Wednesday July 25, 2018 @11:53AM (#17610191)
if (err) return err...

Pointless sugar

Postconditions ala ruby would be "neat", still kinda pointless tho.