Wiki

Changes between Version 10 and Version 11 of ExpressionTour

Show
Ignore:
Timestamp:
05/31/13 09:24:44 (11 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ExpressionTour

    v10 v11  
    218218== Nil == 
    219219 
    220 Operations and literals around nil/null values.[[BR]] 
    221 '''nil''' is the nil/null literal 
    222  
    223 EXPR to ? 
    224   - cast the type of EXPR to the nilable form of the same type (EXPR.typeOf?, nilable EXPR.typeof) 
    225  
    226 EXPR to !  
    227   - cast the type of EXPR to the the non-nilable form of same type( EXPR.typeOf!)  
     220Cobra has various operations and literals around nil/null values.[[BR]] 
     221 
     222'''nil''' is the nil/null literal. 
     223 
     224Cobra provides operators to allow coalescing of expressions that evaluate nil or non nil.[[BR]] 
     225This saves an explicit nil comparison check and assignment in favor of a coalescing expression or  augmented assignment. 
     226 
     227'?' is nil coalesce operator[[BR]] 
     228'!' is non-nil coalesce operator 
    228229 
    229230EXPR ? EXPR1 
     
    234235 
    235236EXPR1 ?= EXPR1 
    236     - nil coalesce EXPR1 to itself or EXPR2 
     237    - augmented assignment: nil coalesce EXPR1 to itself or EXPR2 
    237238    - same as EXPR1 = EXPR1 ? EXPR2 
    238239 
    239240EXPR1 != EXPR1 
    240     - non-nil coalesce EXPR1 to itself or EXPR2 
     241    - augmented assignment: non-nil coalesce EXPR1 to itself or EXPR2 
    241242    - same as EXPR = EXPR1 ! EXPR2 
    242243 
     
    247248#!cobra 
    248249 
    249 if a is nil, print 'Bad a' 
     250if a is nil, print 'Bad a' # explicit comparison and statement 
    250251 
    251252y = a ? b    # coalesce nil : y gets a if a non nil, b otherwise 
     
    264265}}} 
    265266 
     267See also wiki:NilableTypes   
    266268 
    267269== Method References ==