lua-users home
lua-l archive

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


Greetings, all!  I've just discovered this beautiful language quite
recently and am having immense fun figuring out how it behaves. :-)

If someone can spare a moment, would it be possible to confirm a
little detail for me please ...?

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

Is this so?  (I'm not referring to forms in which the condition of
the while is a constant followed by conditional execution or break
in the body, nor am I referring to forms in which a variable in the
condition of the while is set true or false by a condition evaluated
in the body, since neither of these forms carries out the actual test
for termination at the point in the while that was designed for it,
and neither of them can be used symmetrically in if conditionals.)

I guess I was trying to find in the manual some syntactic form which
would convert an assignment into an expression, along the lines of:

                        _,x,_ = foo(), x

or maybe:

                       (_,x,_ = foo(); x)

ie. syntactically something along the lines of (for the 2nd of these):

            <expr> ::= <expr> | '(' {<stat> ';'} <expr> ')'

with an associated semantic of the last <expr> determining the value.
That wouldn't be as elegant nor useful as the assignment yielding a
value directly, but nevertheless it would still be quite practical.

I haven't found any such conversion in the language though, apart from
the functional form that I showed near the start, but I suspect nobody
in their right mind would use that. :-)

A secondary requirement which would be satisfied by the illegal form
is that the scope of a newly created variable could be limited to the
loop, which is thoroughly useful.  Unfortunately, the valid functional
form doesn't help there since the scope of 'x' would have to be outer
to the while altogether, which is less than ideal.

Why my interest in this?  In part because I'm finding my code heavily
populated with duplicated pattern matches, along the lines of:

           if string.find(s,"<lhs><rhs>") then
                _,_,rhs = string.find(s,"<lhs>(<rhs>)")

which is pretty awful on several fronts, when what I need is just:

          if _,_,rhs = string.find(s,"<lhs>(<rhs>)") then

That's invalid of course, but the valid alternative of:

            _,_,rhs = string.find(s,"<lhs>(<rhs>)")
            if rhs then

doesn't limit scope to block of use, and doesn't work for whiles at all.

Any feedback on this would be most welcome.  Meanwhile, I'm enjoying
this voyage of discovery hugely. :-)

Cheers all,

Rich Artym.

-- 
Existing media are so disconnected from reality that our policy debates
spin around a fantasy world in which the future looks far too much like
the past.   http://www.zyvex.com/nanotech/MITtecRvwSmlWrld/article.html