[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: help with lua style and syntax
- From: Egor Skriptunoff <egor.skriptunoff@...>
- Date: Sun, 21 Oct 2012 20:55:06 +0300
On Thu, 18 Oct 2012 13:18 +0100 (BST), David Collier <myshkin@cix.co.uk> wrote:
> Now Lua allows me to return more than one value, so I can write
>
> passed, z = myFunction(x, y)
>
> but is there any way in Lua I can write
>
> if passed, z = myFunction(x, y)
>
> and set both passed and z, but then test 'passed' ??
>
> It would allow me to do:
>
> if ( passed, z = myFunction(x, y) )
> and ( passed, q = mySecondFunction(z) )
> and ( passed, result = myThirdFunction(q) )
>
> which is a lot more comapct than what I'm writing at present.
This problem can be solved by introducing new assignment
operator returning list of values assigned.
Let denote it by ":=" sign:
print(x, y, z := 1, 2) --> 1 2 nil
It would be possible to write compact and easy-readable code:
-- Find any solution of equation a*x^4 + b*x^2 + c == 0, a ~= 0
if (root := sqrt_or_nil(b*b-4*a*c))
and (x := sqrt_or_nil((-b-root)/2/a) or sqrt_or_nil((-b+root)/2/a))
then
print('Solution found: '..x)
else
print('Equation is unsolvable')
end
I believe the ":=" operator will be quite useful in Lua language.