A method represents a routine that can be executed by a class, in the case of static methods, or by an instance of a class, otherwise.
While Pascal and Delphi have a different syntax for declaring procedures and functions, Freya uses the method keyword for both kinds of routines:
Stack = class[X] public // ... method Push(Value: X); method Pop: X; // ... end;
Freya allows three kinds of parameters:
Kind | Prefix | Remarks |
---|---|---|
Value | none | Also called input parameters. A copy of the real argument is passed to the method. |
Reference | var | The method receives a reference to a variable or field, which must have been initialized before the method call. |
Output | out | The method receives a reference to a variable or field. The method must assign a value for this parameter. |
In contrast with Delphi Pascal, you must include the var and out parameter modifiers in a method call:
if dictionary.TryGetValue(key, out result) then // ...
You can specify a method modifier when declaring a method. There are four mutually exclusive method modifiers that control call binding:
Modifier | Purpose |
---|---|
virtual | The implementation of this method can be overridden in a derived class. |
abstract | There's no implementation for this method, and it must be overridden in a derived class. |
override | The implementation of this method overrides the inherited implementation. |
sealed | This previously declared virtual method is overridden the last time in this inheritance branch. |
There's a fifth independent method modifier:
Modifier | Purpose |
---|---|
new | Breaks the implicit relation between this method and a method with the same name in some ancestor class. |
The new modifier can be used alone or combined with virtual or abstract.
Methods are implemented in the implementation section of their declaring class. The method header must be repeated in its implementation, but any method modifier must be omitted:
Stack = class[X] public // ... method Push(Value: X); end; implementation for Stack[X] is method Push(Value: X); begin Items[Count] := Value; Count++; end;
You can write a method implementation without a corresponding method declaration. In that case, the method is considered as private. You can prefix the method header with the static modifier if you want a static private method.
Most executable members in Freya, including properties, operators and methods, can be implemented using an alternative syntax whenever they satisfy these requirements:
Suppose we're writing a Complex type, and we must redefine the GetHashCode method. We can use the following syntax:
public Complex = record public // ... method GetHashCode: Integer; override; end; implementation for Complex is // ... method GetHashCode: Integer => Re.GetHashCode ^ Im.GetHashCode; end.
We have saved ourselves from typing these elements:
We could have directly defined the body when declaring the method:
public Complex = record public // ... method GetHashCode: Integer => Re.GetHashCode ^ Im.GetHashCode; override; end;
Though this feature requires that the body consist in just one expression being assigned to Result, a careful use of condition expressions and common expression blocks allows their use in a wide variety of situations.
The Freya Programming Language
Extension methods
Generic methods
Type declarations
Type members
Constructors
Destructors
User defined operators
Iterators
Fields
Properties
Indexers
Events