Changes between Version 3 and Version 4 of Property
- Timestamp:
- 05/07/12 06:40:06 (13 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Property
v3 v4 3 3 They are accessed using the same syntax as fields but do not designate storage locations.[[BR]] 4 4 Instead, properties have accessors that read, write, or compute their values.[[BR]] 5 Values can be reflected directly (or indirectly (filtered)) in or out of a backing variable or computed, It s entirely up to the accessor.5 Values can be reflected directly (or indirectly (filtered)) in or out of a backing variable or computed, It's entirely up to the accessor. 6 6 7 7 There are 3 keywords for defining a property:[[BR]] … … 22 22 {{{ 23 23 #!cobra 24 var _nLife = 47 # protected access24 var _nLife = 47 # backing variable with (implicit) protected access 25 25 pro nLife from var # property nLife uses backing var _nLife 26 # above same as26 # above line same as 27 27 pro nLife from _nLife 28 # but can use a different backing variable name from property name28 # but can use a different backing variable name from the property name 29 29 pro numLife from _nLife 30 30 31 # or use a backing variable with different ( shortcutted) accessability31 # or use a backing variable with different (also shortcutted) accessability 32 32 var __nLife0 = 48 # private access 33 33 34 pro nLife0 from var # use protected or private backing variable of same name 35 #or alternatively property and backing variable explicitly named 34 36 pro nLife from __nLife0 35 37 }}} 36 38 37 Can implicitly create (a protected) initialised backing variable and property in one line like39 You can implicitly create (a protected) initialised backing variable and property in one line like 38 40 {{{ 39 41 #!cobra … … 42 44 # or explicitly specifying type and initial value 43 45 pro nLife from var as int = 47 46 47 # More commonly perhaps a set or get variant can be given in the same manner 48 ## (assume above prop dcls not made, any one of below is also valid) 49 get nLife from var 50 get nlifeSource from nLife 51 52 set nLife from var # name same as backing variable 53 set nLifeSink from nlife # any name same or unrelated to backing variable 44 54 }}} 45 55 46 56 Finally if an accessor needs to translate, filter or modify in some way a backing variable or provide a virtual variable (computed)[[BR]] 47 57 there is a longer syntax variant where either of both the '''get''' and '''set''' accessors can be specified with a block of code to[[BR]] 48 store (setter) or generate (getter) the property value. 58 store (setter) or generate (getter) the property value. 59 60 Within the setter code block in such a variant an implicit parameter (of the same type as the property) named '''value'''[[BR]] 61 is provided that has the value passed or assigned to the property. 49 62 50 63 {{{ … … 57 70 return 70 - _seed 58 71 set 72 # implicit parameter 'value' contains the value given to set into the property 59 73 require value >0 and value <65 60 74 _seed = value 61 75 }}} 62 76 63 Properties can have Attributes and the default [wiki: accessModifiers access modifiers] can be overridden explicitly if desired.77 Properties can have Attributes and the default [wiki:AccessModifiers access modifiers] can be overridden explicitly if desired. 64 78 65 79 !DocStrings describing the property are both allowed and encouraged.