Product Documentation
Cadence SKILL Language Reference
Product Version IC23.1, November 2023

case

case( 
g_keyForm 
l_clause1 
[ l_clause2 ... ] 
) 
=> g_result / nil

Description

Branches to one of the clauses depending on the value of the given expression. case() evaluates g_keyForm and matches the resulting value sequentially against the clauses until it finds a match. Once a match is found it stops searching the clauses, evaluates the forms in the matching clause, and returns the resulting value. This is a syntax function.

Each l_clause is in turn a list of the form (g_keys g_expr1 [g_expr2 ...]) in which the first element, that is g_keys, is either an atom (that is, a scalar) of any data type or a list of keys (to be compared with the given expression). When using a list of keys, specify it as a list of one or more lists to distinguish it from a list of scalar keys. If any of the keys matches the value from g_keyForm, that clause is selected. Keys are always treated as constants and are never evaluated.

The symbol t has special meaning as a key in that it matches anything. It acts as a catch-all and should be handled last to serve as a default case when no other match is found. To match the value t, use a list of t as the key.

Arguments

g_keyForm

An expression whose value is evaluated and tested for equality against the keys in each clause.

A match occurs when either the selector is equal to the key or the selector is equal to one of the elements in the list of keys. If a match is found, the expressions in that clause and that clause only (that is, the first match) are executed. The value of case is then the value of the last expression evaluated (that is, the last expression in the clause selected). If there is no match, case returns nil.

l_clause1

An expression whose first element is an atom or list of atoms to be compared against the value of g_keyForm. The remainder of the l_clause is evaluated if a match is found.

Do not put quotes or use the list() function when specifying the lists in a clause.

l_clause2

Zero or more clauses of the same form as l_clause1.

Value Returned

g_resultb

The value of the last expression evaluated in the matched clause, or nil if there is no match.

nil

If there is no match.

Examples

nameofmonth = "February"
month = case( nameofmonth
                ("January" 1)
                ("February" 2)
                (t 'Other))
=> 2
listofnums = list(2 4 6)
case( listofnums
 (( (1 2 3) ) 'onetwothree)
 (( (1 3 5)
    (7 9 11) ) 'odd)
 (( (2 4 6)
    (8 10 12) ) 'even)
 (t 'unknown))
=> even
case( myBool
 (nil 'never)  ; this will never match, it is a list of no keys
 (( nil ) nil) ; matches nil
 (( t ) t)     ; matches t
 (t (error "Expected t or nil"))
)
shapeType="line"
rectCount=0
labelOrLineCount=0
miscount=0
case( shapeType 
("rect" ++rectCount println( "Shape is a rectangle" ))
(( "label" "line" ) ++labelOrLineCount println( "Shape is a line or a label" ))
(t ++miscount println( "Shape is miscellaneous" ))
) ; case
=> Shape is a line or a label
procedure(migrateShape(shape "d")
case(list(shape->layerName shape->purpose)
((("POLY" "test1"))
 shape->layerName = "CPO"
shape->purpose = "drawing"
)
((("Oxide" "drawing") ("abcd" "efgh"))
    println(shape->layerName)
)
((("M1" "net"))
    shape->purpose = "label"
)
((("M2" "net"))
    shape->purpose = "label"
)
((("M3" "net"))
    shape->purpose = "label"
)
((("M4" "net"))
    shape->purpose = "label"
)
)
) ;

Related Topics

Flow Control Functions

eq

equal


Return to top
 ⠀
X