Last modified: 2014-09-07 20:18:26 UTC

Wikimedia Bugzilla is closed!

Wikimedia migrated from Bugzilla to Phabricator. Bug reports are handled in Wikimedia Phabricator.
This static website is read-only and for historical purposes. It is not possible to log in and except for displaying bug reports and their history, links might be broken. See T68278, the corresponding Phabricator task for complete and up-to-date bug report information.
Bug 66278 - __call metamethod doesn't work on elements of the export table
__call metamethod doesn't work on elements of the export table
Status: NEW
Product: MediaWiki extensions
Classification: Unclassified
Scribunto (Other open bugs)
unspecified
All All
: Low trivial (vote)
: ---
Assigned To: Nobody - You can work on this!
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2014-06-06 20:37 UTC by Jackmcbarn
Modified: 2014-09-07 20:18 UTC (History)
3 users (show)

See Also:
Web browser: ---
Mobile Platform: ---
Assignee Huggle Beta Tester: ---


Attachments

Description Jackmcbarn 2014-06-06 20:37:52 UTC
Create the following as Module:Foo:
local p, mt = {}, {}

function mt.__call(t, frame)
	return 'Hello from the __call metamethod'
end

p.main = setmetatable({}, mt)

return p

From the sandbox, =p.main(mw.getCurrentFrame()) correctly displays "Hello from the __call metamethod". From wikitext, however, {{#invoke:Foo|main}} displays a script error that p.main isn't a function.
Comment 1 Brad Jorsch 2014-06-06 20:55:51 UTC
What would be the advantage of allowing #invoke to call a table via the __call metamethod, instead of just using a function?

Functions seem like they would be much less confusing to on-wiki module authors than tables-that-can-be-called.
Comment 2 Jackmcbarn 2014-06-06 21:09:01 UTC
Take this module, for example:
local p = {}

p.continents = {
	na = { area = 24490000, pop = 542056000 },
	sa = { area = 17840000, pop = 392555000 },
	antarctica = { area = 13720000, pop = 4490 }
}

p.oceans = {
	atlantic = { area = 82400000, volume = 323600000 },
	pacific = { area = 165250000, volume = 714000000 }
}

local mt = {}

function mt.__call(t, frame)
	for _,v in ipairs(frame.args) do
		t = t[v]
	end
	return t
end

for _,v in pairs(p) do
	setmetatable(v, mt)
end

return p


This module allows other modules to retrieve data via "foo = p.continents.na.area", and wikitext to retrieve data via "{{#invoke:Foo|continents|na|area}}".
Comment 3 Brad Jorsch 2014-06-09 16:19:32 UTC
OTOH, you could as easily write that with a function getContinent, and at the same time avoid confusing people into thinking they could do "{{#invoke:Foo|bar|baz}}" for any random table that is included in 'p'.

For that matter, returning random tables in 'p' may not be the greatest idea; see bug 48393 for example.

Note You need to log in before you can comment on or make changes to this bug.


Navigation
Links