lua-users home
lua-l archive

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


Could you elaborate on the "all sorts of reasons" for not allowing
if-then-else as an expression rather than just a statement?

I've used a number of languages that have an if-then-else expressions,
where something like this:

    return if x = nil then 0 else x end

is allowed. I find it quite useful for handling default values for
uninitialized variables and alternative computations for edge conditions
such as:

    x = if x > max then x = 0 else x = x + 1 end

What I *don't* know is how if-then-else as an expression my negatively
impact existing Lua programs, performance, or style. I haven't written
enough Lua code to have a good feel for that.

Note that an alternative approach would be to implement a special
expression version of if-then-else such as Java's <cond> ? <exp1> :
<exp2> form.

  - Tom Wrensch

On Wed, 27 Jun 2001, Reuben Thomas wrote:

> > if a < b then return 10 else return 25 end
> >
> > but some are more comfortable with other, more terse forms that are not
> > currently supported in Lua.
>
> and indeed (if a<b then 5 else 4) allows if...thens to be expressions rather
> than statements. So you could then imagine writing c = if a <b then 5 else 4
> ...ooh, we're nearly at functional programming. However, it's not at all
> obvious this is a good thing to do to Lua, for all sorts of reasons.
>
> --
> http://sc3d.org/rrt/ | competent, a.  underpromoted
>