Wiki

Changes between Initial Version and Version 1 of IfExpression

Show
Ignore:
Timestamp:
12/20/10 02:05:11 (13 years ago)
Author:
todd.a
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • IfExpression

    v1 v1  
     1The '''if''' expression evaluates to exactly one of two expressions based on a condition. 
     2 
     3=== Grammar === 
     4{{{ 
     5if(<condition>, <texpr>, <fexpr>) 
     6}}} 
     7 
     8If the '''condition''' is true, '''texpr''' is evaluated; otherwise '''fexpr''' is evaluated. The condition is always evaluated and subsequently '''texpr''' or '''fexpr''' will be unless condition threw an exception. 
     9 
     10The type of the '''if''' expression is the greatest common denominator between the type of '''texpr''' and the type of '''fexpr'''. 
     11 
     12Note that using '''if''' expressions to check for '''nil''' is uncommon, since there is a coalesce operator for that purpose which is more succinct and avoids double evaluation. 
     13 
     14{{{ 
     15#!cobra 
     16# Example 1  
     17print if(x>y, x, y)  
     18 
     19# Example 2  
     20print if(value, 'yes', 'no') # type is String  
     21 
     22# Example 3  
     23total += if(direction==DirectionEnum.Long, +1, -1) * amount  
     24 
     25# Example 4  
     26foo = if(condition, 'x', nil) # type is String?  
     27 
     28# Example 5  
     29foo = if(condition, 'x', 5) # type is Object  
     30}}}