Methods in EK9

The method has been covered in classes, traits and components to some degree. They are really only applicable on these aggregate types.

The visibility of methods is covered in class method visibility, trait method visibility and component method visibility,

What are Methods?

They are similar to a function that is bound to some data structure. But the methods have direct access to properties/fields that are defined in that data structure.

The accessibility of methods on classes can be controlled. This makes classes very useful for information hiding. Methods on traits and components are designed to only be a 'public interface'.

Unlike some languages where methods have to be declared 'virtual' to enable them to be overridden, in EK9 all methods can be overridden (except private methods).

Mutator Methods

Some methods on an Object will be used to alter the state of the Object; either directly or by triggering some processing. These are called mutator methods. In general mutator methods check the validity of parameters being passed in and alter the internal state held.

It is expected that such mutator methods will ensure that the internal state of the Object is always valid. In some cases these methods may make deep copies of parameters passed in, they may also throw exceptions in some cases. But importantly they may have side effects, this means they may trigger alterations to overall program state, network traffic or any other processing.

Accessor Methods

Unlike records, where the properties/fields are all public; accessor methods are needed to provide external constructs with access to specific data/state held within and Object.

Processing Methods

There are methods (similar to mutators) that trigger a wide range of processing, they may or may not alter the Object state. 'Drawing' would be be a good example; the internal state of the Object itself might not be altered, but processing has taken place using the internal state to affect the internal state of another Object (say a 'Canvas' for drawing).

Utility Methods

There are also methods that are related to a type of Object that could have been implemented just as functions; but are better linked to an Object type. A good example of this would be type - Time and method - startOfDay().

Any sort of function that is very closely associated with a specific type is a candidate as a utility method. But in most cases functions as a standalone construct are more flexible and reduce coupling.

Defining/Using Methods

Methods must be given a name, though that name can be overloaded (i.e. used again) as long as the number or type or parameters is different. This is unlike functions in a particular module. These must always have a unique name (within that module).

See parameter passing for how parameters are defined on methods and also how parameters are passed to methods.

Calling methods on Objects from within an Object. By using this and super methods can be called explicitly. This is very important when overriding methods, in the case you still want to trigger the functionality in the super.

For explicitly calling methods on traits that an Object implements there is specific syntax, see traits for details on this.

Summary

The example in OptionsHandler shows a number of public and private methods. In general methods and functions are the primary mechanism to trigger some form of processing; with operators providing very specific and well defined processing.

Next Steps

Traits are closely related to classes and are covered next.