Changes between Version 1 and Version 2 of NilableTypes
- Timestamp:
- 05/03/08 14:15:16 (17 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
NilableTypes
v1 v2 40 40 }}} 41 41 42 == nil and not nil detection and coalesce==42 == nil and not nil detection == 43 43 44 44 You can explicitly test for nil or not nil values using normal conditionals ( true is non-nil) … … 54 54 }}} 55 55 56 Nil test and assign or non nil test and process can be done using 'nil coalescing'[[BR]] 56 == nil and non-nil coalesce == 57 Shortcuts for Nil test and assign (coalesce) or non nil test and process [[BR]] 57 58 58 '''?''' coalesce if nil.[[BR]] 59 '''a ? b''' coalesce to b if (a) nil.[[BR]] 60 The expression evaluates to a unless that value is nil, in which case, it evaluates to b.[[BR]] 61 Neither expression will be evaluated more than once and if the first expression is non-nil then b will not be evaluated at all.[[BR]] 62 ( Also the direct augmented assignment version '''a ?= b''' a gets b if a is nil ) 59 63 60 ''' ! ''' execute ifnot nil.64 '''a ! b ''' evaluate b if a not nil. 61 65 {{{ 62 66 stuff as String? = nil … … 64 68 65 69 stuff = stuff ? defaultStuff # coalesce nil 70 # OR stuff ?= defaultStuff 66 71 # equivalent to stuff = if (not stuff, defaultStuff, stuff) 67 72 # or