Logical operators and argument adjustment

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Logical operators and argument adjustment

Alan Watson-2
The logical operators do not seem to interact as I expect
with argument adjustment:

    Lua 3.1 (alpha)  Copyright (C) 1994-1998 TeCGraf
    > function foo() return 0, 1 end
    > a, b = foo()
    > print(a)
    0	
    > print(b)
    1	

    > a, b = nil or foo()
    > print(a)
    0	
    > print(b)
    nil	

    > a, b = 1 and foo()
    > print(a)
    0	
    > print(b)
    nil	

I would have expected the second and third assignments to
have asigned 1 to b.

I came across this while trying to use calls to strfind
linked by or operators to find one of a series of patterns
in a string.

Alan
-- 
Dr Alan Watson
Instituto de Astronomía UNAM

Reply | Threaded
Open this post in threaded view
|

Re: Logical operators and argument adjustment

Roberto Ierusalimschy
> The logical operators do not seem to interact as I expect
> with argument adjustment:

Logical operators, like all operators, adjust their operands to 1 value before
performing any operation. In fact, the only places you can use a function call
and get all its returned values is in an assignment:

 a,...,b = f()

or in a return statement:

  return f()

Any other use of a function call will adjust its results.

-- Roberto