Changes between Version 11 and Version 12 of ExpressionTour
- Timestamp:
- 05/31/13 14:01:06 (11 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
ExpressionTour
v11 v12 100 100 == Casting to Type or Nilability == 101 101 102 Cast an expression to a particular Type (nilable or not). 103 Cast to or away from nilability. 102 You can cast an expression to a particular Type (nilable or not) or cast to or away from nilability. 104 103 105 104 EXPR to TYPE … … 111 110 - ("cast or return nil") 112 111 113 - Note: single token 'to?' no spaces 112 - Note: single token 'to?' no spaces between 'to' and '?' 114 113 115 114 EXPR to ? … … 139 138 s as String? = 'xyzzy' 140 139 sNotNil = s to ! # non nilable 141 140 141 # cast or nil 142 foo = oFoo to? BarType 143 foo ?= defaultBar # nil coalesce nil foo to value of variable defaultBar 144 145 foo = oFoo to? BarType 146 if not foo, print 'cast oFoo to BarType failed' 147 # not foo same as foo == nil 142 148 }}} 143 149 144 150 == 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 ) 151 Provide an expression or alternate depending on whether a conditional-expression evaluates true or false 152 Note: There can be no space between the if and the '(' - single token 'if(' 152 153 153 154 {{{ … … 157 158 assert z == 100 158 159 159 name = if( s.length, s, 'empty') 160 name = if( s.length, s, 'empty') # default name if zero length s 160 161 161 162 o = if(o, o, default) 162 163 # set o to 'default' if it is nil - but see nil coalesce below 163 164 }}} 165 see also IfExpression 164 166 165 167 == 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 168 Apply an expression across a collection or range or a filtered subset thereof returning a new list. 175 169 176 170 {{{ … … 182 176 assert a == [10, 20, 30] 183 177 184 a = for i in 1 0 where i>5 get i178 a = for i in 1:10 where i>5 get i 185 179 assert a == [6, 7, 8, 9] 186 180 … … 194 188 }}} 195 189 190 see also ForExpression 196 191 197 192 == Try-Catch-Get Expression == 198 193 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'. 194 This is a shorthand for a [wiki:Try try-catch] statement wrapping a single expression that may throw an exception. 195 The difference is that it allows an alternative expression to be provided if an Exception is thrown (rather than the Exception)[[BR]] 196 197 The 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'. 201 198 202 199 If an Exception-Type is not given it will catch all Exceptions. 203 200 204 === Grammar ===205 try EXPR catch [ EXCEPTION-TYPE ] get EXPR206 207 201 {{{ 208 202 #!cobra … … 222 216 '''nil''' is the nil/null literal. 223 217 218 {{{ 219 any as Insect? = nil #type is Insect or nil 220 }}} 221 224 222 Cobra 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.223 This saves an explicit nil comparison check and assignment in favor of a coalescing expression or an augmented assignment. 226 224 227 225 '?' is nil coalesce operator[[BR]] … … 234 232 - non-nil coalesce - if EXPR is non nil return EXPR1 235 233 234 augmented assignment uses '?=' and '!=' 235 236 236 EXPR1 ?= EXPR1 237 - augmented assignment:nil coalesce EXPR1 to itself or EXPR2237 - nil coalesce EXPR1 to itself or EXPR2 238 238 - same as EXPR1 = EXPR1 ? EXPR2 239 239 240 240 EXPR1 != EXPR1 241 - augmented assignment:non-nil coalesce EXPR1 to itself or EXPR2241 - non-nil coalesce EXPR1 to itself or EXPR2 242 242 - same as EXPR = EXPR1 ! EXPR2 243 243 … … 265 265 }}} 266 266 267 See also wiki:NilableTypes267 See also NilableTypes 268 268 269 269 == Method References ==