lua-users home
lua-l archive

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


On Saturday, 20.06.2009, 22:44 +0100 Peter Cawley wrote:
        > a, b, c = (function() return 1, 2, 3, 4, 5 end)()
        > =a, b, c
        1       2       3
        
        As previously stated, in table constructors, each expression
        except
        the final expression is adjusted to exactly one result. The
        final
        expression is not adjusted at all - if it returns no values,
        then it
        adds to values to the table.

Thanks for answering. Sorry, I cannot not get the point. 

I have a problem to understand the fact, that Lua makes - to my
understandig - a difference in functions. If I use the funktion
math.sin() from the Library or the function print() that makes a
difference:
>  a, b, c = (function() return math.sin(1),print("Hallo"), 2, 3, 4, 5
end)()
Hallo
> return a,b,c
0.8414709848079	nil	2
>
In my head-picture the function math.sin() delivers a return value and
function print() does that as well. So there is only exactly one result
in any of the two cases. The one (math.sin(1) gives 0.841... (for the
argument used in the example here) and the other (print("Hello")) should
give Hello. The one will do it as often as i like it: 
> return a,b,c
0.8414709848079	nil	2
> return a,b,c
0.8414709848079	nil	2
> 
The other only will do only once when I declarate the table (see above)
and will deliver nil afterwards. 
I can do the declaration without a function as well:
> a,b,c = math.sin(1),print("Hallo")
Hallo
> return a,b,c
0.8414709848079	nil	nil
> 
There the same question arises: one return function in both functions -
one does work the other not.(That the last, say c -> nil is the result
one will expect and beyond of the actual question)  

One migt illustrate the questionable behaviour in another way: 
> return type(math.cos(1))
number
> return type(print(1))
1
stdin:1: bad argument #1 to 'type' (value expected)
stack traceback:
	[C]: in function 'type'
	stdin:1: in main chunk
	[C]: ?
> 
Should`nt in both cases the result be "number"? If one tries to get an
explanation another way:
> return math.sin
function: 0x8cd5700
> return print
function: 0x8cd2020
> return type(math.cos)
function
> return  type(print)
function
> 
there the output is correct and as expected and as I think, explainable.
(In my original posting there was an error with a parantehesis that was
shadowing the essentail point of question - sorry)

I have a severe problem in understandig that different behaviour of Lua
with function, with Lua that should deal equal. 

Nice sunday anyway, regards BB