Page MenuHomePhabricator

Order of named parameters passed to parser functions is lost
Open, MediumPublicFeature

Description

It's impossible to convert the statement frame:preprocess('{{#switch:' .. v .. '|red|orange=1|yellow|green=2|blue|indigo=3|violet=4|5}}') to use frame:callParserFunction. Here are two attempts that don't work:

frame:callParserFunction('#switch', {v, 'red', 'orange=1', 'yellow', 'green=2', 'blue', 'indigo=3', 'violet=4', '5'})

This one doesn't work because the values aren't split at the equals sign, which isn't a bad thing.

frame:callParserFunction('#switch', {v, 'red', orange='1', 'yellow', green='2', 'blue', indigo='3', violet='4', '5'})

This one doesn't work because Lua can't maintain order in an associative array, which is the cause of this bug. To fix this, I propose the following new syntax:

frame:callParserFunction('#switch', {v, 'red', {'orange', '1'}, 'yellow', {'green', '2'}, 'blue', {'indigo', '3'}, {'violet', '4'}, '5'})

Although only parser functions can "see" order like this, I also propose that for consistency's sake, all brace-substitution functions (newChild, expandTemplate, etc.) should support taking arguments in this format, or a similar one that accomplishes the same thing.

Details

Reference
bz70077

Event Timeline

bzimport raised the priority of this task from to Medium.Nov 22 2014, 3:30 AM
bzimport added a project: Scribunto.
bzimport set Reference to bz70077.
bzimport added a subscriber: Unknown Object (MLST).
Tacsipacsi changed the subtype of this task from "Task" to "Feature Request".
Tacsipacsi subscribed.

Why would you want to use #switch in Lua? You can just use Lua control statements (ifelseifelse), which works as expected, and is less expensive (no calls into PHP) and much more readable (it’s not obvious from either of the above three syntax versions that red will map to 1, yellow will map to 2 and blue will map to 3 – in wikitext, there’s less distance between the value and the equals sign, and yet one usually needs to play with spacing to make the meaning obvious).

This is not to say that this feature shouldn’t be implemented, but IMHO we’d need a more compelling reason.