Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
freem
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Module:Yesno/doc
(section)
Add languages
Module
Discussion
English
Read
Edit source
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit source
View history
General
What links here
Related changes
Special pages
Page information
Appearance
move to sidebar
hide
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Handling nil results=== 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 the 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. (XXX: when value is false, 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> 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 -- Terse coding when the default is false: myvariable = yesno(value or false, false) -- if value is nil, yesno() is called with false instead; result is false. -- If value is 'foo', yesno() is called with 'foo'; 'foo' makes no sense, so the default (false) is returned. -- if value is 'yes' or 'no', yesno() is called with 'yes' or 'no'; result is true or false. -- Terse coding when the default is true: myvariable = yesno(value or true, true) -- if value is nil, yesno() is called with true instead; result is true. -- If value is 'foo', yesno() is called with 'foo'; 'foo' makes no sense, so the default (true) is returned. -- if value is 'yes' or 'no', yesno() is called with 'yes' or 'no'; result is true or false. </syntaxhighlight><!-- --><includeonly>{{sandbox other|| [[Category:Lua metamodules]] }}</includeonly> <noinclude> [[Category:Module documentation pages]] </noinclude>
Summary:
Please note that all contributions to freem are considered to be released under the Creative Commons Attribution-ShareAlike 4.0 (see
Freem:Copyrights
for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)