lua-users home
lua-l archive

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


On Thu, Feb 18, 2016 at 13:36:40 +1100, Daurnimator wrote:
> > It's the classic idiom "test and value_if_true or value_if_false",
> > just extended with two tests. For reference, the function posted by
> > Mason (plus some line breaks and indentation to eliminate hard line
> > wraps) was:
> >
> > function __index(self, k)
> >     return type(k) == "number" and k <= #self and 0/0 or nil
> > end
> >
> > First, 'type(k) == "number"' is tested. If false, then the binary
> > operation 'type(k) == "number" and k <= #self' short-circuits and
> > returns its first argument, the boolean false. Then 'false and 0/0'
> > short-circuits, returning its first argument (false). That leaves
> > 'false or nil', which returns its second argument, nil, as the result
> > of the whole chain of operations.
> >
> > If type(k) is a number, though, then 'type(k) == "number" and k <=
> > #self' returns the result of 'k <= #self'. If that is false, then the
> > next 'and' operation short-circuits as above, and nil is again the
> > final result. If true, then you get 'true and 0/0', which results in
> > the truthy '0/0', which then short-circuits the final op.
> >
> > Does that all make sense now?
> >
> 
> Yep. sorry, had a brain fail :)

And this is why the keyboard gods put parentheses into our keymaps and
Roberto let us use them to disambiguate our code.

D.

-- 
Daniel Silverstone                         http://www.digital-scurf.org/
PGP mail accepted and encouraged.            Key Id: 3CCE BABE 206C 3B69