lua-users home
lua-l archive

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


> Is it the case that the only way to express the *invalid* form:
>
>                  while x = foo() do print(x) end
>
> (in which the focus is on the assignment being performed at the
> while condition control point) is by using the valid Lua form:
>
>     while (function() x=foo(); return x end)() do print(x) end

Basically, yes.  You could define the function first and use
it in the while loop (which saves on function creations.)

   do
     local function test() x = foo() return x end
     while test() do print(x) end
   end

I've requested something similar a long time ago but didn't
get any comments back then.  My suggestion was that as much
constructions as possible should be an expression.  This
should include blocks (that evaluate to their final
expression.)  Then your example could become:

    while x = foo() x do print(x) end

If assignment is turned into an expression leaving its
adjusted values as a result then it could be 

    while x = foo() do print(x) end

(though I personally like the previous example better for
obvious reasons.)

Also I'd like to see "if .. then" constructions as an
expression as well, so you can write

    if x then aap() else noot() end

which evaluates to either one of the expressions (that can
be nil.)  How to turn loops into expressions is less clear
and there will no doubt be other problems related to this
"expressionism", such as how to handle a break inside a
block.

As I wrote before, turning blocks into expressions would be
a nice start!

Bye,
Wim