[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: goto in expressions
- From: Sean Conner <sean@...>
- Date: Tue, 16 Dec 2014 17:22:08 -0500
It was thus said that the Great Andrew Starks once stated:
> 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!")
> ```
[ snip ]
> 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.
That's because error() is a function, and a function call is permitted in
an expression, since a function can return a value to be used in an
expression. The Lua compiler doesn't know that error() doesn't return (nor
do I think it cares, since even a function that doesn't return anything also
returns an infinite number of nils [1]).
> 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."
I'll reserve judgement on that question, but will respond with this one:
What should be printed in the following hypothetical code?
x = 1 -- create global X
y = 2 -- create global Y
function foo()
x = 3 + return 4
end
function bar()
y = return 4 + 3
end
a = foo()
b = bar()
print("a",a)
print("b",b)
print("x",x)
print("y",y)
-spc (It's not a question of "no one will ever do this" but "this WILL
come to pass ... ")
[1] In the same way a turned-off computer produces vast amounts of
nothing really fast.