lua-users home
lua-l archive

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


Hi,

i prefer default-values using metatables:

local _snddefaults = {__index = {loop = true}}
setmetatable(sounds[1], _snddefaults) 
...


Jürgen

On Wed, Sep 13, 2006 at 11:52:45AM -0500, Thomas Blom wrote:
> Hello,
> 
> Simple question here:
> 
> sounds =
> {
>   { name="beep", file="./media/beep.wav" },
>   { name="bloop", file="./media/bloop.wav", loop=false },
> }
> 
> If I want the parameter "loop" for a sound to default to true when  
> unspecified I thought I would write:
> 
> shouldLoop = sounds[1].loop or true  -- (nil or true) => true
> 
> but then it doesn't work for the second entry:
> 
> shouldLoop = sounds[2].loop or true  -- (false or true) => true
> 
> This was a bit of a surprise, since (nil or false) => false.
> 
> 
> What's the elegant idiom for this situation?
> 
> 
> 
> 
> 
> -- 
> Using Opera's revolutionary e-mail client: http://www.opera.com/mail/