Wiki
Version 6 (modified by hopscc, 11 years ago)

--

Methods

Methods are blocks of code specific to a class. They can be thought of as providing the actions that a class instance can be told to do.

Methods can take zero or a number of parameters passed to them and may return a value.

Methods within a class are defined using the keyword def.

If not explicitly specified, the returnType of a method is void (i.e nothing returned) and the access modifiers are public, virtual.

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.
If the name is prefixed with __ its access modifier defaults to private.

Without either of those prefixes on the name of the method, the accessType is public, virtual and it must be accessed from within method code by prefixing the name with either this. or just . (dot) .

Method Grammar

    def <methodName> [as <returnType>]  [is <AccessModifiers>]
        [has <Attributes>]
        [<DocString>]
    def <methodName>(<paramList>) [as <returnType>]  [is <AccessModifiers>]
        [has <Attributes>]
        [<DocString>]

Method ParamList

A parameter list is a comma separated list of name (and optionally type and parameter description modifier) specifications

<paramName> [as [<paramDesc>] <Type>]  [, ...]

<paramDesc> is optional and may be vari and/or a parameter direction indicator out or inout ( default if unspecified is in)

vari indicates the parameter name is a placeholder for a variable length arglist. Within the method this may be unpacked/accessed as a list.

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)

out the arg is returned from the method

inout argument is both passed into the method and (any possibly changed) value is also returned from the method (pass-by-reference)

If <Type> is unspecified it is defaulted to and treated as dynamic.

Example

    def meth( a, b is String, c is out String)
        c = b + "_meth"

    def sum(a as vari int) as int
        sum = 0
        for i in a
            sum += i
        return sum

meth takes 3 args , the first and second are inward only and are typed as dynamic and a string respectively,
the third is only returned from the method as a string.

sum takes a variable number of integer args and returns an integer.

Special Methods

These are named methods on a class that have some special responsibility or relationship to the Object Model
(they were originally implemented as methods with special names but have since been redone specified as cues

They include:

  • Initializer init

The initializer (or constructor) method of a class is a special method named init which is implicitly called when a class instance is constructed. It's responsible for setting the contents of a class instance to an initial state. It doesnt return any value and is not inherited (i.e baseclass initialization must be done with an explicit call to a baseclass constructor).

If an init method is not specified the compiler runtime generates a noArg constructor that does nothing (changed - this was original behaviour, now an init method must be explicitly provided).

Constructor chaining can be done to another constructor in the same class (usually with a different number or Type of args) or to a Superclass constructor (using base.init) - this call must be the first executable line in the init method.

  • Destructor finalize
  • HashCode calculation hash hash as int
  • Comparison compare compare(other) as int
  • Equality equals equals(other) as int
  • Enumerator enumerate enumerate as T*