Difference between revisions of "Case"
From OpenEUO
					
										
					
					| m | m | ||
| Line 1: | Line 1: | ||
| − | ''case  | + | == 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 == | ||
| + | |||
| + | * [[case.on]] | ||
| + | |||
| + | == 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, [[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. | ||
| + | |||
| + | == Alternative Calling Patterns == | ||
| + | |||
| + | == Upon Error == | ||
| == See Also == | == See Also == | ||
| * [http://www.easyuo.com/openeuo/wiki/index.php/Simplelib simplelib] | * [http://www.easyuo.com/openeuo/wiki/index.php/Simplelib simplelib] | ||
| + | |||
| + | * [[case.on]] | ||
Revision as of 10:12, 2 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 [[[case.on|on]] method selects a function from the initial case table and calls it with the remaining parameters passed through.
