| 1 | = Methods = |
| 2 | |
| 3 | Methods within a class are defined using the keyword '''def'''. |
| 4 | |
| 5 | If not specified the returnType is void (i.e nothing returned) and the [wiki:AccessModifiers access modifiers] are '''public, virtual'''[[BR]] |
| 6 | |
| 7 | Like variables, if the name is prefixed with an '''_''' its access modifier defaults to ''protected'' and it is directly accessable from within the class method code.[[BR]] |
| 8 | If the name is prefixed with '''!__''' its access modifier defaults to ''private''. |
| 9 | |
| 10 | Without that prefix on the name of the method the accessType is ''public, virtual'' and |
| 11 | it must be accessed from within method code by prefixing the name with either '''this.''' or just '''.''' .[[BR]] |
| 12 | |
| 13 | == Method Grammar == |
| 14 | {{{ |
| 15 | def <methodName> [as <returnType>] [is <AccessModifiers>] |
| 16 | [has <Attributes>] |
| 17 | [<DocString>] |
| 18 | def <methodName>(<paramList>) [as <returnType>] [is <AccessModifiers>] |
| 19 | [has <Attributes>] |
| 20 | [<DocString>] |
| 21 | }}} |
| 22 | |
| 23 | |
| 24 | === Method ParamList === |
| 25 | |
| 26 | A parameter list is a comma separated list of name (and optionally type and parameter description modifier) specifications |
| 27 | |
| 28 | {{{ |
| 29 | <paramName> [as [<paramDesc>] <Type>] [, ...] |
| 30 | }}} |
| 31 | <paramDesc> is optional and may be '''vari''' and/or a parameter direction indicator '''out''' or '''inout''' ( default if unspecified is '''in''')[[BR]] |
| 32 | |
| 33 | '''vari''' indicates the parameter name is a placeholder for a variable length arglist. Within the method this may be unpacked/accessed as a list.[[BR]] |
| 34 | |
| 35 | '''in''' (implicit) Args are only passed into the method. Any changes made to the argument inside the method are not visible outside the method (pass-by-value)[[BR]] |
| 36 | |
| 37 | '''out''' the arg is returned from the method [[BR]] |
| 38 | |
| 39 | '''inout''' argument is both passed into the method and (any possibly changed) value is also returned from the method (pass-by-reference)[[BR]] |
| 40 | |
| 41 | |
| 42 | If <Type> is unspecified it is treated as dynamic?[[BR]] |
| 43 | |
| 44 | |
| 45 | == Example == |
| 46 | {{{ |
| 47 | def meth( a, b is String, c is out String) |
| 48 | c = b + "_meth" |
| 49 | |
| 50 | def sum(a as vari int) as int |
| 51 | sum = 0 |
| 52 | for i in a |
| 53 | sum += i |
| 54 | return sum |
| 55 | }}} |
| 56 | |
| 57 | meth takes 3 args , the first and second are inward only and are typed as dynamic and a string respectively, [[br]] |
| 58 | the third is only returned from the method as a string. |
| 59 | |
| 60 | sum takes a variable number of integer args and returns an integer. |
| 61 | |
| 62 | == Special Methods == |
| 63 | These are named methods on a class that have some special responsibility or relationship to the Object Model[[BR]] |
| 64 | (they were originally implemented as methods with special names but have since been redone specified as [wiki:Cue cues] |
| 65 | |
| 66 | They include: |
| 67 | |
| 68 | * Initializer '''init''' |
| 69 | |
| 70 | The __initializer__ (or constructor) method of a class is a special method named '''init''' which is implicitly called when a class instance is constructed. |
| 71 | It's responsible for setting the contents of a class instance to an initial state. |
| 72 | It doesnt return any value and is not inherited (i.e baseclass initialization must be done with an explicit call to |
| 73 | a baseclass constructor). |
| 74 | |
| 75 | If an init method is not specified the compiler runtime generates a noArg constructor that does nothing. |
| 76 | |
| 77 | Constructor chaining can be done to another constructor in the same class (usually with a different number or Type of args) |
| 78 | or to a Superclass constructor (using '''base.init''') - this call must be the first executable line in the init method. |
| 79 | |
| 80 | * Destructor '''finalize''' |
| 81 | |
| 82 | * !HashCode calculation '''hash''' hash as int |
| 83 | |
| 84 | * Comparison '''compare''' compare(other) as int |
| 85 | |
| 86 | * Equality '''equals''' equals(other) as int |
| 87 | |
| 88 | * Enumerator '''enumerate''' enumerate as T* |
| 89 | |