[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: goto in expressions
- From: Andrew Starks <andrew.starks@...>
- Date: Tue, 16 Dec 2014 16:03:11 -0600
On Tue, Dec 16, 2014 at 1:57 PM, Sean Conner <sean@conman.org> wrote:
> -spc (Is my narrative good enough?)
The basics of your answer were known to me, but the details are helpful.
I was thinking of this in a way that is related to something else that
I do, all of the time:
```
local x = x or error("You must have a value for x!")
```
Like `error`, `goto` doesn't return. The human reading the code does
not bother to ask "what happens to x?" because that human knows that
the statement didn't finish:
```
local x = false
local function fun()
x= x or error('x must not be set!')
end
local success, ret = pcall(fun)
print(success, x)
--> false, false
```
The added benefit is that nothing past the truth-y value is evaluated,
which is nice for code that is called all of the time (like the setup
of a tight loop).
I see that in the case of `error`, the rule regarding bare expressions
is not broken. I'm hoping for the same thing with `goto`, except I
acknowledge that it's an expression.
Also, this logically begs the question, if this were legal,
could/would this also be legal:
```
x = x or return nil
```
Maybe this is just a fundamentally bad idea? That is: "good code
doesn't do this."
Thank you for your answer. I primarily wanted to point out that
"return value" doesn't really play into what `goto` does for a living.
-Andrew