assignments in expressions?

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

assignments in expressions?

Lenny Palozzi-3
In the code below I would like to assign 'c' the return value of function
'f', and have c(or the return value of function 'f') compared to the
right-hand side. From the looks of the Lua syntax, you can't do this in Lua.

I get:

error: `)' expected;
  last token read: `=' at line 6 in file `t2.lua'

Can Lua be easily changed to handle this? Just curious, as I sometimes write
similar code in C.

function f()
  return 1
end

c=0
while (c=f()) == 1 do
  print(c)
end

Thanks,
-Lenny


Reply | Threaded
Open this post in threaded view
|

Re: assignments in expressions?

Steve Williams
Why the need to do that?  That leads to hard-to-read code that is no more
efficient than a easier to understand layout.  Are we going to have an
Obfuscated Lua Code Contest?

What is wrong with this way?  It is no different in functionality and easier
to understand.

c = f()
while c == 1 do
  print(c)
  c = f()
end

Steve 'Sly' Williams - Code Monkey - http://www.kromestudios.com

> In the code below I would like to assign 'c' the return value of function
> 'f', and have c(or the return value of function 'f') compared to the
> right-hand side. From the looks of the Lua syntax, you can't do this in
Lua.
>
> I get:
>
> error: `)' expected;
>   last token read: `=' at line 6 in file `t2.lua'
>
> Can Lua be easily changed to handle this? Just curious, as I sometimes
write
> similar code in C.
>
> function f()
>   return 1
> end
>
> c=0
> while (c=f()) == 1 do
>   print(c)
> end



Reply | Threaded
Open this post in threaded view
|

RE: assignments in expressions?

Ashley Fryer-2
Now that we have a "break" statement, you can eliminate the duplicate
assignment by doing this:

while 1 do
    c = f()
    if c == 1 then break end
    print(c)
end


-----Original Message-----
From: Steve Williams
Sent: Thursday, January 11, 2001 12:56 PM
Subject: Re: assignments in expressions?

What is wrong with this way?  It is no different in functionality and easier
to understand.

c = f()
while c == 1 do
  print(c)
  c = f()
end




Reply | Threaded
Open this post in threaded view
|

Re: assignments in expressions?

Reuben Thomas-3
In reply to this post by Steve Williams
> Why the need to do that?  That leads to hard-to-read code that is no more
> efficient than a easier to understand layout.  Are we going to have an
> Obfuscated Lua Code Contest?

Let me say that while I do think every statement should return a value, I'm
not in favour of inlined assignments. Although I do like the ability to
cascade assignments:

a = b = c = 1;

But once you've got every statement returning a value, it's perhaps a little
hard to prevent the horrible C style of assignments (which I have to admit I
use in C, for brevity and efficiency).

-- 
http://sc3d.org/rrt/
L'art des vers est de transformer en beautés les faiblesses (Aragon)