Editing
Module:Buffer/doc
(section)
From Thetacola Wiki
Jump to navigation
Jump to search
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!
====_G object==== The first _G variable [[#initialize|passed to this module]] is given a __call metamethod that self-{{luaref|rawset||y}}s and returns in a manner that depends on whether it was called directly or from a chain.<ref group="example">Saving a new_G object globally via a chain call can prevent conflict. The follow example has a hypothetical "Module:X" that may overwrite globals declared by your module or unwittingly discard your new_G when it passes _G to Module:Buffer (passing _G to this module resets new_G to the global table even when the global functions are already enabled): :{| |{{#tag:syntaxhighlight| return require'Module:Buffer'(_G)--Return before require to show intent to return a Buffer object; chain cannot be broken :_R(frame.args.title and --store values outside global scope if invoked with title parameter 'new_G') :_G'myBuff' --save current Buffer in new_G :_2'new_G' --retrieve new_G :_G'my_G' --save new_G as global my_G :_G('t', --save title object as my_G.t for later re-use mw.title.new(frame.args.title or frame.args.page) ).myBuff --go to my_G.myBuff (my_G lacks the Buffer:_2 method, but doesn't need it) :stream(my_G.t.exists --just arbitrary code to show how in-line storage may be used without breaking the chain or warning(my_G.t), --local function warning() declared before return require'Module:X'.main(my_G.t), my_G.t.isSubpage and subpage(my_G.t), ... ) :_R('new_G', my_G) --set my_G as new_G again and have the new_G from Module:X as its metaindex :_(frame.args.detail and my_G.info :_(frame.args.detail)--append Buffer object from Module:X's new_G.info if args.details and it exists; append detail param to result or my_G.summary) --just append summary from Module:X if not invoked with detail param. :_B(t and --use global t as a shorthand for "if not frame.args.title then" (t only declared a global in line 2 if no title given) myBuff :stream(frame.args.page, frame.args.details2, mw.html.create'br', require'Module:Y'.main(frame)) or my_G.myBuff --place results in a table if invoked with title param (alternative chain call branches within Buffer:_B) :_inHTML'table'(_addArgs) :_parent() ) |lang=lua}} |}</ref> This module conserves any pre-existing metatable and alters no metamethod other than __call. :{| | =====direct call===== <code>_G( k, v )</code><br /> <code>_G'string'</code> When called, the _G object self-sets any string passed as <code>k</code> with whatever is passed as <code>v</code>. This returns ''v'', or nil if omitted (unlike with rawset, an explicit nil is not necessary to unset a variable with direct calls). Note that ''k'' must be a string to declare or unset a global in this op. Tables passed as the first argument are treated as though this were executed via a call chain (discussed shortly). Passing ''k'' which is not one of those two types will throw an error. =====chain call===== <code>chained-object:_G( k, v )</code><br /> <code>chained-object:_G'string'</code> {{distinguish|text=[[#Buffer:_G|§ Buffer:_G]], the Buffer object function, which differences are noted in the final paragraph of this section.}} When used in a call chain, this rawsets the ''k''ey-''v''alue pair in the chained object and returns that object. The _G object may chain itself when returning _G is desired for another op instead of ''v''. In contrast to the direct op, the in-chain op will index non-string ''k'' values. Moreover, this only unsets object[k] when passed an explicitly nil ''v''. If ''v'' is omitted in-chain, this uses the chained object as the missing argument; thus, (chained) <code>object:_G'string'</code> has identical effect and return to <code>_G('string', object)</code>. |} The same __call method is given to [[#new_G|new_G]] objects created by Buffer:_R, however the direct call only works if its metaindex is the _G object. Any table such that <code>table._G</code> points to the _G object may chain save to itself regardless of metaindex. Though the behavior of the chain op when ''v'' is omitted may be a [[Dead ringer (idiom)|dead ringer]] to that of [[#Buffer:_G|Buffer:_G]] when ''save'' is omitted and [[#new_G|new_G]] is the chained object, mind that the Buffer object function sets keys in new_G variable rather than the chained (Buffer) object; in other words, this is unaffected by Buffer:_R reassigning new_G to another table. Also, this does not have the back up behavior of Buffer:_G. {{anchor|Buffer-variable}}
Summary:
Please note that all contributions to Thetacola Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, 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 (see
Project:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Navigation menu
Page actions
Module
Discussion
Read
Edit source
History
Page actions
Module
Discussion
More
Tools
Personal tools
Not logged in
Talk
Contributions
Create account
Log in
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Search
Tools
What links here
Related changes
Special pages
Page information