Wiki

Changes between Version 7 and Version 8 of ExpressionTour

Show
Ignore:
Timestamp:
05/28/13 13:20:19 (11 years ago)
Author:
hopscc
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • ExpressionTour

    v7 v8  
    4747 
    4848== Bitwise == 
    49  
    50 to-do (& | ^ << >> ~) 
     49Standard bit operations. 
     50  - & = bitwise 'and' 
     51  - | = bitwise 'or' 
     52  - ~ = bitwise not (flip) 
     53  - !^ = bitwise Xor 
     54  - << = left shift 
     55  - >> = right shift  
     56 
    5157 
    5258== Boolean == 
     
    8995to-do (inherits, implements, if-inherits, alternative is virtual method) 
    9096 
    91 == Casting == 
    92  
    93 to-do (to, to?) 
     97== Casting to Type or Nilability == 
     98 
     99Cast an expression to a particular Type (nilable or not). 
     100Cast to or away from nilability. 
     101 
     102EXPR to TYPE  
     103   - cast the expression to the given TYPE, cast failing throws an Exception. 
     104   - ("cast or throw Exception") 
     105    
     106EXPR to? TYPE 
     107   - cast the expression to the given TYPE, if the cast fails return nil  
     108   - ("cast or return nil") 
     109 
     110   - Note: single token 'to?' no spaces 
     111 
     112EXPR to ? 
     113  - cast the type of EXPR to the nilable version of same type  (EXPR.typeOf?) 
     114  - Note space between 'to' and '?' 
     115 
     116EXPR to !  
     117  - cast the type of EXPR to the non-nilable version of same type( EXPR.typeOf!)  
     118 
     119{{{ 
     120#!cobra 
     121 
     122s = 'String' 
     123sObj = s to Object 
     124assert sObj.typeof is Object 
     125 
     126ds as dynamic = 'xyzzy' 
     127s = ds to String 
     128 
     129i = 99 
     130s = i to? String 
     131assert s is nil 
     132 
     133s = 'xyzzy'  # nonnilable 
     134sNil = s to ? #cast to nilable 
     135 
     136s as String? = 'xyzzy' 
     137sNotNil = s to ! # non nilable 
     138  
     139}}} 
    94140 
    95141== If Expression == 
    96  
    97 to-do 
     142Trinary operator on an expression. 
     143If the conditional expression evaluates to true  give the first Expr otherwise give the second.  
     144 
     145Note: no space between the if and the '(' - single token 'if(' 
     146 
     147=== Grammar === 
     148if( COND-EXPR, EXPR1, EXPR2 ) 
     149 
     150{{{ 
     151#!cobra 
     152i=22 
     153z = if( i>10, 100, 0)  
     154assert z == 100 
     155 
     156name = if( s.length, s, 'empty') 
     157 
     158o = if(o, o, default) 
     159# set o to 'default' if it is nil - but see nil coalesce below 
     160}}} 
    98161 
    99162== For Expression == 
    100  
    101 to-do 
     163Apply an expression across a collection or range or a filtered subset thereof. 
     164 
     165=== Grammar === 
     166for VAR in EXPR [where EXPR ] get EXPR 
     167 
     168for VAR in EXPR where EXPR  
     169  - same as   for VAR in EXPR where EXPR get VAR 
     170 
     171for VAR in START-EXPR : STOP-EXPR [: STEP_EXPR] [where EXPR] get EXPR  
     172 
     173{{{ 
     174#!cobra 
     175a = for i in 1:4 get i*2 
     176assert a == [2,4,6] 
     177 
     178a = for i in [1, 2, 3] get i*10 
     179assert a == [10, 20, 30]   
     180 
     181a = for i in 10 where i>5 get i 
     182assert a == [6, 7, 8, 9] 
     183 
     184c = ['how', 'now', 'brown', 'cow'] 
     185d = for k in c where k.endsWith('ow') get k 
     186assert d == ['how', 'now', 'cow'] 
     187 
     188 
     189tmpFiles = for fileName in Directory.getFiles('.') where '.tmp' in fileName 
     190 
     191}}} 
     192 
     193 
     194== Try-Catch-Get Expression == 
     195 
     196This is a shorthand for a try-catch statement wrapping a single statement assigning an expression.[[BR]] 
     197The 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'. 
     198 
     199If an Exception-Type is not given it will catch all Exceptions. 
     200  
     201=== Grammar === 
     202try EXPR catch [ EXCEPTION-TYPE ] get EXPR  
     203 
     204{{{ 
     205#!cobra 
     206inVal = '..' #an int string presumably 
     207x = try int.parse(inVal) catch FormatException get 0 #  return 0 if inVal not an int String 
     208 
     209# Catch all exceptions 
     210dflt = '---' 
     211s1 = try String.format('{1:P}', 0.123) catch get dflt 
     212assert s1 == '---' 
     213}}} 
    102214 
    103215== Nil == 
    104216 
    105 to-do (nil, to ?, to !, coalesce ? and ?=, ! and !=) 
     217Operations and literals around nil/null values.[[BR]] 
     218'''nil''' is the nil/null literal 
     219 
     220EXPR to ? 
     221  - cast the type of EXPR to the nilable form of the same type (EXPR.typeOf?, nilable EXPR.typeof) 
     222 
     223EXPR to !  
     224  - cast the type of EXPR to the the non-nilable form of same type( EXPR.typeOf!)  
     225 
     226EXPR ? EXPR1 
     227   - nil coalesce - if EXPR is nil return EXPR1 
     228 
     229EXPR ! EXPR 
     230   - non-nil coalesce - if EXPR is non nil return EXPR1 
     231 
     232EXPR1 ?= EXPR1 
     233    - nil coalesce EXPR1 to itself or EXPR2 
     234    - same as EXPR1 = EXPR1 ? EXPR2 
     235 
     236EXPR1 != EXPR1 
     237    - non-nil coalesce EXPR1 to itself or EXPR2 
     238    - same as EXPR = EXPR1 ! EXPR2 
     239 
     240     - Note: gotcha Warning 
     241     b != a   This is NOT 'b not equals (<>) a' -  is same as b = b ! a  OR  b = if( b <> nil, a, nil) 
     242 
     243{{{ 
     244#!cobra 
     245 
     246if a is nil, print 'Bad a' 
     247 
     248y = a ? b    # coalesce nil : y gets a if a non nil, b otherwise 
     249#same as y = if(a, a, b) except a evaluated only once 
     250 
     251a = a ? default    # a if a nonnil, default otherwise 
     252# above is same as  
     253a ?= default 
     254 
     255 
     256y = b ! a    # coalesce non nil : y gets b if b is nil, a otherwise   
     257# same as y = if(b <> nil, a, nil)  OR y = if(not b, a, nil) 
     258 
     259i as int? = input ! int.parse(input)   
     260#( equiv to i = if(input <> nil ? int.parse(input),  nil) 
     261}}} 
     262 
    106263 
    107264== Method References ==