Difference between revisions of "Case.on"

From OpenEUO
Jump to: navigation, search
m (Calling Pattern)
m (Example Usage)
Line 23: Line 23:
 
  c.on(z,2,3,4,5,6)
 
  c.on(z,2,3,4,5,6)
  
  --> 'c'
+
  --> c
 
  --> 5
 
  --> 5
  

Revision as of 16:46, 2 November 2010

Calling Pattern

Call

local c = sl.case(casetable)
-- later
local r[,...] = c.on(sel, arg[, ...])

Args

sel is any type, used as key to lookup in casetable
arg0..argN are variadic arguments of any type

Results

r0...rN are the indefinite results of applying arg0...argN to the value found in casetable

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

The method on searches for sel among the keys of casetable. If found and the associated value is a function, then the remaining variadic arguments arg0...argN (if any) are passed to that function and its results are returned as the results to the on invocation. If sel is found but the associated value is not a function, then the actual value is returned as the result of the on invocation. If sel isn't found, then a magic value, 'default' is searched for and if found then its associated value is treated in an identical manner.

Upon Error

If neither the passed sel key or the magic 'default' key are found among the keys of casetable, then an error is reported and handled according to the operant error redirection mode.

See Also