Module:Yesno/doc: Difference between revisions

Content deleted Content added
I have just made many attempts, there is no code that handles the "nil" case properly AND simply; as a reminder, Lua's "and/or pseudo-ternary" doesn't exactly behaves like real "?:" ternaries, and that's problematic here; I would suggest to handle the "nil" case really separately, e.g. « if value == nil then myvariable = true/false else myvariable = yesno(value) end », as it's the simplest actually
Handling nil results: added better suggestions
Line 110:
myvariable = yesno('foo') or false -- Unknown string returns nil, result is false.
myvariable = yesno('foo', true) or false -- Default value (here: true) applies, result is true.
</syntaxhighlight>
 
Better suggestions:
<syntaxhighlight lang="lua">
local myvariable = yesno(value)
if myvariable == nil then -- value is nil or an unrecognized string
myvariable = true
end
 
-- more efficient when value is nil, but more verbose
-- (note the default result has to be written twice)
local myvariable
if value == nil then
myvariable = true
else
myvariable = yesno(value, true)
end
</syntaxhighlight><!--