Content deleted Content added
+2 new examples |
preventing nil-result (see also talk) |
||
Line 91:
</syntaxhighlight>
Although the empty string usually evaluates to false in wikitext, it evaluates to true in Lua. This module prefers the Lua behaviour over the wikitext behaviour. If treating the empty string as false is important for your module, you will need to convert empty strings to a value that evaluates to false before passing them to this module. In the case of arguments received from wikitext, this can be done by using [[Module:Arguments]].
===nil returns===
By definition
:<syntaxhighlight lang="lua">
yesno(nil) -- Returns nil.
yesno('foo') -- Returns nil.
yesno(nil, true) -- Returns nil.
yesno(nil, false) -- Returns nil.
yesno('foo', true) -- Returns true.
</syntaxhighlight>
To get binary <syntaxhighlight lang="lua" inline>true / false</syntaxhighlight>-only values, use code like:
<syntaxhighlight lang="lua">
myvariable = yesno(value) or false -- When value is nil, result is false.
myvariable = yesno(value) or true -- When value is nil, result is true.
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><!--
--><includeonly>{{sandbox other|sandbox||
[[Category:Lua metamodules]]
}}</includeonly>
|