Case.on

From OpenEUO
Revision as of 13:37, 2 November 2010 by Ximan (Talk | contribs) (Created page with "== 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 Results r0......")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

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

Results

r0...rN are the indefinite results of applying arg1...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