User:Ganaram inukshuk/Provisional style guide for Lua: Difference between revisions

Ganaram inukshuk (talk | contribs)
Ganaram inukshuk (talk | contribs)
No edit summary
Line 98: Line 98:
=== Use of functions ===
=== Use of functions ===


===== "Main", wrapper, and tester functions =====
===== Modules that implement templates =====
Templates should consist of at least two functions: a "main" function prefixed with an underscore, and a wrapper function of the same name but without an underscore.
Modules that implement templates should consist of at least two functions: a "main" function prefixed with an underscore, and a wrapper function of the same name but without an underscore.
 
The use of a wrapper and "main" function allows for a module to be used directly in another module or indirectly through its corresponding template. A module should only provide '''one''' wrapper for '''one''' template. If the "main" function is short enough, its code may be merged with the wrapper function.
 


The use of a wrapper and "main" function allows for a module to be used directly in another module or indirectly through its corresponding template. A module should only provide '''one''' wrapper for '''one''' template.


A tester function may be added for testing purposes, which is itself a wrapper that also calls the "main" function. This allows it to be tested using the in-browser console by calling mw.logObject(p.tester()). Tester functions may be removed if the "main" function is determined to be functional under expected conditions.<syntaxhighlight lang="lua">-- "Main" function to be called by wrapper or another module
A tester function may be added for testing purposes, which is itself a wrapper that also calls the "main" function. This allows it to be tested using the in-browser console by calling mw.logObject(p.tester()). Tester functions may be removed if the "main" function is determined to be functional under expected conditions.<syntaxhighlight lang="lua">-- "Main" function to be called by wrapper or another module
Line 108: Line 110:
end
end


-- Wrapper function; to be called by template
-- Wrapper function; to be called by template using {{#invoke}}
function p.call_me(frame)
function p.call_me(frame)
     return p._call_me(frame.args)
     return p._call_me(frame.args)
Line 117: Line 119:
     local args = { ["something"] = 123 }
     local args = { ["something"] = 123 }
     return p._call_me(args)
     return p._call_me(args)
end</syntaxhighlight>The guidelines stated above do not apply to the following:
end</syntaxhighlight><syntaxhighlight lang="lua">-- Wrapper function; to be called by template
function p.call_me(frame)
    local arg = frame.args["arg"]
   
    -- Code goes here
   
    return call_me(arg)
end</syntaxhighlight>
 
==== Modules that serve as libraries ====
Modules that serve as libraries for other modules do not need a wrapper function, apart from the tester. Examples include [[Module:MOS]], [[Module:Rational]], and [[Module:Utils]].
 
Modules that serve as libraries for multiple, related templates may provide multiple wrapper functions. If a module provides multiple wrappers that each call a corresponding function, the name of the function called within the wrapper must be prefixed with an underscore.<syntaxhighlight lang="lua">-- MAIN FUNCTIONS
-- Usable by other modules
 
function p._clamp(arg1, arg2, arg3)
    -- Code goes here
end
 
function p._truncate(arg1)
    -- Code goes here
end
 
-- WRAPPER FUNCTIONS
-- Usable by templates with {{#invoke}}
 
function p.clamp(frame)
    return p._clamp(frame.args["arg1"], frame.args["arg2"], frame.args["arg3"])
end


* Modules that serve as libraries for other modules; in such cases, there is no wrapper function other than the tester function. Example: [[Module:MOS]] and [[Module:Rational]]
function p.truncate(frame)
* Modules that provide multiple functions, including wrapper functions, meant to be invoked by multiple templates; in such cases, the rule regarding one wrapper for one template does not apply. Example: [[Module:Rational]] and [[Module:Utils]]
    return p._truncate(frame.args["arg1"])
* Modules that provide only one function for other modules; in such cases, no wrapper functions are necessary. Example: [[Module:Yesno]]
end</syntaxhighlight>Modules that only provide one function may simply consist of that function as a return value. Example: [[Module:Yesno]].
* Template-based modules where writing a separate "main" function produces a short function; in such cases, the code for that function may be merged with the wrapper function. Example: [[Module:MOS scalesig]]
* Template-based modules whose wrapper function calls another module's functions; in such cases, no other functions are necessary. Example: [[Module:MOS scalesig]]


==== Helper functions ====
==== Helper functions ====