[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Default value in function parameters?
- From: Philippe Lhoste <PhiLho@...>
- Date: Wed, 20 Sep 2006 09:54:07 +0200
Grellier, Thierry a écrit :
So, I would prefer to be able to write something like (VHDL flavor, and
yet another even more disturbing keyword :-) knowing that if elseif...
could be reused)
style = "native"
a = { a = 1,
b = { b = 2,
c = 1 when style == "native",
2 when style == "rock",
0 else
}
}
rather than
[...]
style = "native"
a = { a = 1,
b = { b = 2, c = f(style)
}
}
function f(style)
...
end
which disseminates the code, and well looks like coding.
In an application I made with Lua, I used functions in tables in a
similar way, except that functions were evaluated each time.
a =
{
a = 1,
b =
{
b = 2,
c = function (style)
if style == "native" then return 1
elseif style == "rock" then return 2
else return 0
end
end
}
}
Slightly more verbose than your proposal, but still bearable and using a
familiar syntax.
It was a kind of database of city ways, the field indicated which postal
area a way belong to. For some ways, it depends on the number of address
in the way, eg. odd vs. even, below or above a value, and so on.
When I needed the postal address, I checked the type of the field: if
numerical, just use it; if it is a function, evaluate it.
Worked well. No "dissemination". Really nice, showing the flexibility of
Lua.
Your need is slightly different as you want to evaluate only once, at
construction time. Should I have to do it, I suppose I would walk the
table and evaluate the functions I find to fix the values.
I made a typo while writing this code, typing
if style = "native" then
Since assignments are not expressions, I wonder why the == syntax was
chosen over the simpler and unambiguous = (beside the familiarity for C
users...).
--
Philippe Lhoste
-- (near) Paris -- France
-- http://Phi.Lho.free.fr
-- -- -- -- -- -- -- -- -- -- -- -- -- --