lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, Dec 16, 2014 at 4:22 PM, Sean Conner <sean@conman.org> wrote:
> 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]).

Yes. I understand this part and accepted it as a change. I'm wondering
if there is an implication apart from pointing out that this would be
a change?

>
>> 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)
>

I believe that I demonstrated this answer. Is this example
fundamentally different:

```
local x = false
local y = true
local function fun()
   x, y =  x or error('x must not be set!'), 'foo', 'bar'
end
local success, ret = pcall(fun)
print(success, x, y)
--> false, false, true
```

That is, I did this because `error` is legal. The same sort of
behavior could be imagined for goto, WITH the added limitation that
(perhaps) the "local" would not be allowed here? That is, you couldn't
create new locals on the left and have a goto happen on the right?

I don't mean to keep this up, except that I may not be getting the
significance behind the particular rule that you're pointing out, at
least as something that "cannot ever be broken under any
circumstance".

Perhaps this is really off base, but `and`, `or` and `not` are all
"naked" and allowed.

Perhaps you could use these keywords as examples, by explaining how
that case is different?

-Andrew
>   -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.
>