91 | | == Casting == |
92 | | |
93 | | to-do (to, to?) |
| 97 | == Casting to Type or Nilability == |
| 98 | |
| 99 | Cast an expression to a particular Type (nilable or not). |
| 100 | Cast to or away from nilability. |
| 101 | |
| 102 | EXPR to TYPE |
| 103 | - cast the expression to the given TYPE, cast failing throws an Exception. |
| 104 | - ("cast or throw Exception") |
| 105 | |
| 106 | EXPR 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 | |
| 112 | EXPR to ? |
| 113 | - cast the type of EXPR to the nilable version of same type (EXPR.typeOf?) |
| 114 | - Note space between 'to' and '?' |
| 115 | |
| 116 | EXPR to ! |
| 117 | - cast the type of EXPR to the non-nilable version of same type( EXPR.typeOf!) |
| 118 | |
| 119 | {{{ |
| 120 | #!cobra |
| 121 | |
| 122 | s = 'String' |
| 123 | sObj = s to Object |
| 124 | assert sObj.typeof is Object |
| 125 | |
| 126 | ds as dynamic = 'xyzzy' |
| 127 | s = ds to String |
| 128 | |
| 129 | i = 99 |
| 130 | s = i to? String |
| 131 | assert s is nil |
| 132 | |
| 133 | s = 'xyzzy' # nonnilable |
| 134 | sNil = s to ? #cast to nilable |
| 135 | |
| 136 | s as String? = 'xyzzy' |
| 137 | sNotNil = s to ! # non nilable |
| 138 | |
| 139 | }}} |
100 | | |
101 | | to-do |
| 163 | Apply an expression across a collection or range or a filtered subset thereof. |
| 164 | |
| 165 | === Grammar === |
| 166 | for VAR in EXPR [where EXPR ] get EXPR |
| 167 | |
| 168 | for VAR in EXPR where EXPR |
| 169 | - same as for VAR in EXPR where EXPR get VAR |
| 170 | |
| 171 | for VAR in START-EXPR : STOP-EXPR [: STEP_EXPR] [where EXPR] get EXPR |
| 172 | |
| 173 | {{{ |
| 174 | #!cobra |
| 175 | a = for i in 1:4 get i*2 |
| 176 | assert a == [2,4,6] |
| 177 | |
| 178 | a = for i in [1, 2, 3] get i*10 |
| 179 | assert a == [10, 20, 30] |
| 180 | |
| 181 | a = for i in 10 where i>5 get i |
| 182 | assert a == [6, 7, 8, 9] |
| 183 | |
| 184 | c = ['how', 'now', 'brown', 'cow'] |
| 185 | d = for k in c where k.endsWith('ow') get k |
| 186 | assert d == ['how', 'now', 'cow'] |
| 187 | |
| 188 | |
| 189 | tmpFiles = for fileName in Directory.getFiles('.') where '.tmp' in fileName |
| 190 | |
| 191 | }}} |
| 192 | |
| 193 | |
| 194 | == Try-Catch-Get Expression == |
| 195 | |
| 196 | This is a shorthand for a try-catch statement wrapping a single statement assigning an expression.[[BR]] |
| 197 | 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'. |
| 198 | |
| 199 | If an Exception-Type is not given it will catch all Exceptions. |
| 200 | |
| 201 | === Grammar === |
| 202 | try EXPR catch [ EXCEPTION-TYPE ] get EXPR |
| 203 | |
| 204 | {{{ |
| 205 | #!cobra |
| 206 | inVal = '..' #an int string presumably |
| 207 | x = try int.parse(inVal) catch FormatException get 0 # return 0 if inVal not an int String |
| 208 | |
| 209 | # Catch all exceptions |
| 210 | dflt = '---' |
| 211 | s1 = try String.format('{1:P}', 0.123) catch get dflt |
| 212 | assert s1 == '---' |
| 213 | }}} |
105 | | to-do (nil, to ?, to !, coalesce ? and ?=, ! and !=) |
| 217 | Operations and literals around nil/null values.[[BR]] |
| 218 | '''nil''' is the nil/null literal |
| 219 | |
| 220 | EXPR to ? |
| 221 | - cast the type of EXPR to the nilable form of the same type (EXPR.typeOf?, nilable EXPR.typeof) |
| 222 | |
| 223 | EXPR to ! |
| 224 | - cast the type of EXPR to the the non-nilable form of same type( EXPR.typeOf!) |
| 225 | |
| 226 | EXPR ? EXPR1 |
| 227 | - nil coalesce - if EXPR is nil return EXPR1 |
| 228 | |
| 229 | EXPR ! EXPR |
| 230 | - non-nil coalesce - if EXPR is non nil return EXPR1 |
| 231 | |
| 232 | EXPR1 ?= EXPR1 |
| 233 | - nil coalesce EXPR1 to itself or EXPR2 |
| 234 | - same as EXPR1 = EXPR1 ? EXPR2 |
| 235 | |
| 236 | EXPR1 != 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 | |
| 246 | if a is nil, print 'Bad a' |
| 247 | |
| 248 | y = 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 | |
| 251 | a = a ? default # a if a nonnil, default otherwise |
| 252 | # above is same as |
| 253 | a ?= default |
| 254 | |
| 255 | |
| 256 | y = 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 | |
| 259 | i as int? = input ! int.parse(input) |
| 260 | #( equiv to i = if(input <> nil ? int.parse(input), nil) |
| 261 | }}} |
| 262 | |