Wiki

Changes between Version 11 and Version 12 of ExpressionTour

Show
Ignore:
Timestamp:
05/31/13 14:01:06 (12 years ago)
Author:
hopscc
Comment:

Cleanup and minimise as a tour

Legend:

Unmodified
Added
Removed
Modified
  • ExpressionTour

    v11 v12  
    100100== Casting to Type or Nilability == 
    101101 
    102 Cast an expression to a particular Type (nilable or not). 
    103 Cast to or away from nilability. 
     102You can cast an expression to a particular Type (nilable or not) or cast to or away from nilability. 
    104103 
    105104EXPR to TYPE  
     
    111110   - ("cast or return nil") 
    112111 
    113    - Note: single token 'to?' no spaces 
     112   - Note: single token 'to?' no spaces between 'to' and '?' 
    114113 
    115114EXPR to ? 
     
    139138s as String? = 'xyzzy' 
    140139sNotNil = s to ! # non nilable 
    141   
     140 
     141# cast or nil 
     142foo = oFoo to? BarType  
     143foo ?= defaultBar # nil coalesce nil foo to value of variable defaultBar 
     144 
     145foo = oFoo to? BarType  
     146if not foo, print 'cast oFoo to BarType failed'  
     147# not foo same as foo == nil 
    142148}}} 
    143149 
    144150== If Expression == 
    145 Trinary operator on an expression. 
    146 If the conditional expression evaluates to true  give the first Expr otherwise give the second.  
    147  
    148 Note: no space between the if and the '(' - single token 'if(' 
    149  
    150 === Grammar === 
    151 if( COND-EXPR, EXPR1, EXPR2 ) 
     151Provide an expression or alternate depending on whether a conditional-expression evaluates true or false 
     152Note: There can be no space between the if and the '(' - single token 'if(' 
    152153 
    153154{{{ 
     
    157158assert z == 100 
    158159 
    159 name = if( s.length, s, 'empty') 
     160name = if( s.length, s, 'empty') # default name if zero length s 
    160161 
    161162o = if(o, o, default) 
    162163# set o to 'default' if it is nil - but see nil coalesce below 
    163164}}} 
     165see also IfExpression 
    164166 
    165167== For Expression == 
    166 Apply an expression across a collection or range or a filtered subset thereof. 
    167  
    168 === Grammar === 
    169 for VAR in EXPR [where EXPR ] get EXPR 
    170  
    171 for VAR in EXPR where EXPR  
    172   - same as   for VAR in EXPR where EXPR get VAR 
    173  
    174 for VAR in START-EXPR : STOP-EXPR [: STEP_EXPR] [where EXPR] get EXPR  
     168Apply an expression across a collection or range or a filtered subset thereof returning a new list. 
    175169 
    176170{{{ 
     
    182176assert a == [10, 20, 30]   
    183177 
    184 a = for i in 10 where i>5 get i 
     178a = for i in 1:10 where i>5 get i 
    185179assert a == [6, 7, 8, 9] 
    186180 
     
    194188}}} 
    195189 
     190see also ForExpression 
    196191 
    197192== Try-Catch-Get Expression == 
    198193 
    199 This is a shorthand for a try-catch statement wrapping a single statement assigning an expression.[[BR]] 
    200 The value of the expression is the value of the expr following 'try' or if an exception is thrown the value of the expression after the 'get'. 
     194This is a shorthand for a [wiki:Try try-catch] statement wrapping a single expression that may throw an exception.  
     195The difference is that it allows an alternative expression to be provided if an Exception is thrown (rather than the Exception)[[BR]] 
     196 
     197The value of the expression is the value of the expression following 'try' or if an exception is thrown the value of the expression after the 'get'. 
    201198 
    202199If an Exception-Type is not given it will catch all Exceptions. 
    203200  
    204 === Grammar === 
    205 try EXPR catch [ EXCEPTION-TYPE ] get EXPR  
    206  
    207201{{{ 
    208202#!cobra 
     
    222216'''nil''' is the nil/null literal. 
    223217 
     218{{{ 
     219any as Insect? = nil  #type is Insect or nil 
     220}}} 
     221 
    224222Cobra provides operators to allow coalescing of expressions that evaluate nil or non nil.[[BR]] 
    225 This saves an explicit nil comparison check and assignment in favor of a coalescing expression or augmented assignment. 
     223This saves an explicit nil comparison check and assignment in favor of a coalescing expression or an augmented assignment. 
    226224 
    227225'?' is nil coalesce operator[[BR]] 
     
    234232   - non-nil coalesce - if EXPR is non nil return EXPR1 
    235233 
     234augmented assignment uses '?=' and '!=' 
     235 
    236236EXPR1 ?= EXPR1 
    237     - augmented assignment: nil coalesce EXPR1 to itself or EXPR2 
     237    - nil coalesce EXPR1 to itself or EXPR2 
    238238    - same as EXPR1 = EXPR1 ? EXPR2 
    239239 
    240240EXPR1 != EXPR1 
    241     - augmented assignment: non-nil coalesce EXPR1 to itself or EXPR2 
     241    - non-nil coalesce EXPR1 to itself or EXPR2 
    242242    - same as EXPR = EXPR1 ! EXPR2 
    243243 
     
    265265}}} 
    266266 
    267 See also wiki:NilableTypes   
     267See also NilableTypes   
    268268 
    269269== Method References ==