Difference between revisions of "Case"
m (→Description) |
(→Alternative Calling Patterns) |
||
(4 intermediate revisions by one other user not shown) | |||
Line 24: | Line 24: | ||
c.on(z,2,3,4,5,6) | c.on(z,2,3,4,5,6) | ||
− | --> | + | --> c |
− | + | 5 | |
== Description == | == Description == | ||
− | Calling case returns a case closure with one defined method, [[case.on|on]]. This closure can stand in place of complicated if-elseif code and functions analogously to the C language switch case construct except that it can be called anywhere once defined. The first parameter passed to the [[case.on|on]] method selects a function from the initial case table and calls it with the remaining parameters passed through. If the selector isn't found among the case table keys, then the default key 'default' is searched for and if found, the associated function is called. If neither the selector value | + | Calling case returns a case closure with one defined method, [[case.on|on]]. This closure can stand in place of complicated if-elseif code and functions analogously to the C language switch case construct except that it can be called anywhere once defined. The first parameter passed to the [[case.on|on]] method selects a function from the initial case table and calls it with the remaining parameters passed through. If the selector isn't found among the case table keys, then the default key 'default' is searched for and if found, the associated function is called. If the associated value is not of type 'function', the actual value is returned. If neither the selector value nor 'default' is found in the case table, then an error is raised. ''fixed in version 0.04'' |
== Alternative Calling Patterns == | == Alternative Calling Patterns == | ||
+ | |||
+ | Multiple case tables can be passed as arguments; if later tables in the argument sequence have the same keys as earlier ones, the later tables' values take precedence in the generated closure casetable. In call with multiple tables, all keys that are not numbers and not strings are converted first to strings for the internal case table. | ||
+ | |||
+ | Call | ||
+ | local c = sl.case(casetable[, ...]) | ||
+ | |||
+ | Multiple functions can be passed as arguments, in which case the first argument is assigned to key [1] in the generated closure casetable, and the Nth argument is assigned to the Nth key, and so on. The last function passed is also assigned to the 'default' key. | ||
+ | |||
+ | Call | ||
+ | local c = sl.case(function[, ...]) | ||
== Upon Error == | == Upon Error == | ||
+ | |||
+ | If none of the described argument patterns are matched, an error is reported and handled according to the operant error redirection mode. | ||
== See Also == | == See Also == |
Latest revision as of 13:12, 22 November 2010
Contents
Calling Pattern
Call
local c = sl.case(casetable)
Args
casetable is a table, where keys are any type and values are functions
Results
c is a case closure
Closure Methods
Example Usage
local t = { [1] = function(...) local a={...} print('a') print(a[a[5] ]) end, ['1'] = function(...) local a={...} print('b') print(a[a[4] ]) end, bye = function(...) local a={...} print('c') print(a[a[3] ]) end, default = function(...) local a={...} print('d') print(a[a[2] ]) end, } local c = sl.case(t) local z = 'bye' c.on(z,2,3,4,5,6)
--> c 5
Description
Calling case returns a case closure with one defined method, on. This closure can stand in place of complicated if-elseif code and functions analogously to the C language switch case construct except that it can be called anywhere once defined. The first parameter passed to the on method selects a function from the initial case table and calls it with the remaining parameters passed through. If the selector isn't found among the case table keys, then the default key 'default' is searched for and if found, the associated function is called. If the associated value is not of type 'function', the actual value is returned. If neither the selector value nor 'default' is found in the case table, then an error is raised. fixed in version 0.04
Alternative Calling Patterns
Multiple case tables can be passed as arguments; if later tables in the argument sequence have the same keys as earlier ones, the later tables' values take precedence in the generated closure casetable. In call with multiple tables, all keys that are not numbers and not strings are converted first to strings for the internal case table.
Call
local c = sl.case(casetable[, ...])
Multiple functions can be passed as arguments, in which case the first argument is assigned to key [1] in the generated closure casetable, and the Nth argument is assigned to the Nth key, and so on. The last function passed is also assigned to the 'default' key.
Call
local c = sl.case(function[, ...])
Upon Error
If none of the described argument patterns are matched, an error is reported and handled according to the operant error redirection mode.