111 | | Define a property for the class |
112 | | |
113 | | ''To be expanded'' |
114 | | |
115 | | === Properties Grammar === |
116 | | |
117 | | {{{ |
118 | | get <propName> [ as {<Type>, var}] [is <accessModifier>] |
119 | | set <propName> [ as {<Type>, var} [is <accessModifier>] |
120 | | |
121 | | pro <propName> [ as {<Type>, var}] [is <accessModifier>] |
122 | | |
123 | | pro <propName> [is <accessModifier>] |
124 | | |
| 111 | ''This discussion is somewhat simplified for clarity.[[BR]] |
| 112 | Please see |
| 113 | [wiki:Property Properties]'' |
| 114 | |
| 115 | Properties are members that provide a flexible mechanism to read, write, or compute the values of private fields. [[BR]] |
| 116 | They are used as if they are public data members, but are actually implemented via special methods called accessors. |
| 117 | |
| 118 | There are 3 keywords for defining a property:[[BR]] |
| 119 | '''get''' defines a readOnly property ( Can access the properties value but cant change it). [[BR]] |
| 120 | '''set''' defines a writeOnly property (Can write to the property but cant read it)[[BR]] |
| 121 | '''pro''' defines a readable and writable property. |
| 122 | |
| 123 | If a property accessor merely reflects a protected or private backing variable (field) you can[[BR]] |
| 124 | use a clause beginning with keyword '''from'''. |
| 125 | * '''from var''' [[BR]] |
| 126 | {{{ pro <name> from var }}} or {{{get <name> from var}}} or {{{ set <name> from var}}}[[BR]] |
| 127 | The property will get or set its value unchanged from a backing variable named |
| 128 | to the property name with a single underscore prepended to it. |
| 129 | * If there is no backing variable but there is an assignment to the property or a type declaration on the property |
| 130 | the backing variable will be implicitly created for you. |
| 131 | * ''' from <backingVarName>'''[[BR]] |
| 132 | Otherwise the word following the '''from''' must be the name of an existing backing store variable. |
| 133 | |
| 134 | Finally if an accessor needs to translate, filter or modifies in some way a backing variable or computes a value[[BR]] |
| 135 | there is a longer syntax variant where either of both the get and set accessors can be specified and a block of code[[BR]] |
| 136 | provided to store (setter) or generate (getter) the property value. |
| 137 | |
| 138 | === (Simplified) Properties Grammar === |
| 139 | |
| 140 | {{{ |
| 141 | get <propName> [ as <Type>] [from {var, <backingVariableName>} [= <initValue>] ] |
| 142 | [<DocString>] |
| 143 | |
| 144 | set <propName> [ as <Type>] [from {var, <backingVariableName>} [= <initValue>] ] |
| 145 | [<DocString>] |
| 146 | |
| 147 | pro <propName> [ as <Type>] [from {var, <backingVariableName>} [= <initValue>] ] |
| 148 | [<DocString>] |
| 149 | |
| 150 | # full accessor variant, one or both of 'get' and 'set blocks must be present. |
| 151 | pro <propName> |