Dodo doc > Types > Constructors and Methods > Instance Methods


Instance Methods

Methods are similar to functions with the addition that they can be overloaded to allow multiple argument lists. Methods build an object as their return value. While functions are generally stateless methods are generally stateful.

Syntax

A method name follows the same syntax as a type name. It must start with an upper case letter and can contain letters, digits and underscore "_".

Examples:

Reset
Mix
Start_process
Rationale: as we will see later, a method declaration is actually a type declaration. This is why the syntax is the same. Reusing an existing concept helps in keeping the language simple.

Declaration

A method declaration starts with "method" followed by the method name, then a declaration block. Inside the declaration block, the method can have different bodies each with its own set of parameters.

A method body declaraction starts with the method name followed by the list of parameters, much in the same way as a constructor is declared (see section on constructors). In fact, a method declaration is exactly the same as a type declaration with constructors, a type which prototype is "method". A method does not have a default value.

There can be other declarations in the method declaration block which are accessed via the "self" variable inside a method body. Member variables can serve as the return values of the method.

Examples:

method Mix: Mix(.Fruit[], SpeedSetting?).

method Play
{
Play(Sound, .Output, Player.Options?)
Play(Video, .Window, Player.Options?)
enum result
}
Rationale: it is convenient to use the type declaration syntax for methods as dodo methods share many characteristics with dodo constructors. Both can take different sets of parameters, both create an object and both can modify state. Since the no-argument body is not called to create a default value, method calls are always explicit. The declaration of return values in the method block allows multiple return values.

Invocation

A method invocation uses the dot to connect the object variable with the method name. The method name is followed by the list of arguments. An empty list of arguments is written with empty brackets.

A method can only be invoked on a variable reference. It can be a variable declared in the scope or an argument passed by reference. Methods cannot be used on constants.

To read the attributes of the object built by the method, the result of method invocation can be saved into a variable. This is a way to handle multiple return values.

Examples:

foodProcessor.Mix(.fruits, 9)
player.Reset()
def play = player.Play(mp3file, soundOut, [:low_latency, :simple_convol])
Rationale: since a method call can modify the object it is invoked on, the object needs to be a reference. The syntax is same as a function call (except that empty brackets cannot be left out), which makes methods easy to use. Here we see the benefit of calling a constructor without "new" in dodo.

^ 4.6.1. Constructors

v 4.7. Type Compatibility