Wiki

Changes between Version 2 and Version 3 of CoalesceExpression

Show
Ignore:
Timestamp:
07/07/13 13:27:14 (11 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • CoalesceExpression

    v2 v3  
    1 = Coalesce Expression = 
     1= Coalesce Expressions = 
     2There are two forms of this - nil-coalesce and nonNil-coalesce.  
     3In both cases they evaluate the first expression and if it is nil (nonNil) return the second expression otherwise return the first expression.  
    24 
    3 The '''coalesce''' binary expression evaluates to the first non-nil value. There is an augmented assignment version of it as well. 
     5They are convenient shorthand for respectively testing an expression against nil and returning a default or  
     6testing against nonNil and returning something else (usually a deref of the first expression) 
     7 
     8= Nil Coalesce = 
     9  
     10The '''nil coalesce''' binary expression evaluates to the first non-nil value. There is an augmented assignment version of it as well. 
    411 
    512=== Grammar === 
     
    1017The expression evaluates to a unless that value is '''nil''', in which case, it evaluates to '''b'''. 
    1118 
    12 Although uncommon, nothing prevents b itself from also being '''nil'''. Neither expression will be evaluated more than once and if a is non-nil then b will not be evaluated at all. 
     19Although uncommon, nothing prevents b itself from also being '''nil'''.  
     20Neither expression will be evaluated more than once and if a is non-nil then b will not be evaluated at all. 
    1321 
    1422The type of the coalesce expression is the greatest common denominator between the type of '''a''' and the type of '''b'''. 
     
    1927}}} 
    2028 
    21 In the augmented assignment version, the result is assigned back to a. This requires that b is type compatible with a or a compilation error will occur. 
     29In the augmented assignment version, the result is assigned back to a.  
     30This requires that b is type compatible with a or a compilation error will occur. 
    2231 
     32=== Examples === 
    2333{{{ 
    2434#!cobra 
    2535# Example 1  
    2636print name ? 'NONAME'  
     37 
    2738 
    2839# Example 2  
     
    3243 
    3344# Example 3  
    34 get name as String 
    35     return _name ? .getType.name  
    36  
    37 # Example 4  
    3845# this:  
    3946name = if(employee.manager.name<>nil, employee.manager.name, 'NONAME')  
    4047# can evaluate the key expression twice and is less succinct than:  
    4148name = employee.manager.name ? 'NONAME'  
     49 
    4250}}} 
     51 
     52 
     53= !NonNil Coalesce = 
     54  
     55The '''nonNil coalesce''' binary expression evaluates the second expression if the first is a non-nil value. 
     56If the first expression is nil it just returns it.  
     57There is an augmented assignment version of it as well. 
     58 
     59=== Grammar === 
     60{{{ 
     61<a> ! <b> 
     62}}} 
     63 
     64The expression evaluates to a if that value is '''nil''', otherwise it evaluates to '''b'''. 
     65 
     66Although uncommon, nothing prevents b itself from also being '''nil'''.  
     67Neither expression will be evaluated more than once and if a is nil then b will not be evaluated at all. 
     68 
     69The type of the coalesce expression is the greatest common denominator between the type 'nil'  and the type of '''b'''. 
     70(Nilable Type of '''b''') 
     71 
     72=== Grammar === 
     73{{{ 
     74<a> != <b> 
     75}}} 
     76 
     77In the augmented assignment version, the result is assigned back to a.  
     78This requires that b is type compatible with a or a compilation error will occur. 
     79 
     80Note: this looks like a boolean comparison (not equals) in other languages. 
     81 
     82{{{ 
     83#!cobra 
     84# Example 1  
     85print x ! x.name # if x is not nil return its member name else nil 
     86 
     87# name or default regardless of x or x.name nil 
     88print x ! x.name ? 'NONAME'  
     89 
     90# Example 2  
     91current = list 
     92current != current.head # (not a comparison)  nil if list nil or list.head nil 
     93if current 
     94    # process 
     95 
     96# Example 3  
     97name = if(employee and employee.manager and employee.manager.name, employee.manager.name, 'NONAME')  
     98# is less succinct than:  
     99name = employee ! employee.manager ! employee.manager.name ? 'NONAME'  
     100# assumes derefs are idempotent (same returns on each call or deref) 
     101 
     102# Example 4 
     103#if calls are NOT idempotent need to capture each deref and use it subsequently 
     104# assuming employee.manager returns nil or a different value on each visit as in some places 
     105mgrName = employee ! (m = employee.manager) ! m.name ? 'NONAME'  
     106}}}