Methods

From The Oxygene Language Wiki

Jump to:navigation, search

This is a Language topic
Feel free to add your notes to this topic below.



A method is a block of code that can be called elsewhere within the application. Methods are a replacement for Procedures and Functions defined by basic Pascal.

Interface Syntax

[class] method methodname [parameters]  [: result] ; [modifiers;]

where the square brackets denote optional parts of the declaration

Implementation Syntax

[class] method classname.methodname [parameters]  [: result] ;
begin
  [methodbody]
end;

Note the differences from the interface declaration:

The method's parameters must be repeated though - they form part of the methods's signature and the compiler is able to support implicit overloading. Thus the Delphi Prism language has no need of an overload keyword

If you prefer, you can supply procedure or function instead of method and the compiler will check for the appropriate existence of a returned result.

A method can use the self keyword to refer to its owning class.

Method Visibility

For methods within classes, see Class Member Visibility Levels. Global methods are different though, because you have no access to the implicit Global Class to set their visibility. Instead, Delphi Prism provides an optional assembly or public suffix:

method Test(a: String): integer; public;
begin
  ...
end;

In common with all types, the default visibility is assembly, so you don't actually need to provide that suffix yourself. However, you must remember to provide 'public' if you require access to the method outside the assembly.

Method Parameters

As stated above, method parameters for the implementation need to match the interface declaration and method overloading is supported implicitly. See Parameters for a description of the parameter syntax and rules.

Method Result

If the method returns a result, an implicit Result variable is created for the method (also see exit).

Method Modifiers

See Class Member Modifiers

Class Methods

Specifying class before method has two important consequences:

1. the method can be called regardless of whether an instance of the class has been created 2. as there is no guarantee of an instance, the method cannot reference any fields

Unsafe Methods

In order to support Unsafe Code, methods need to be flagged as unsafe, e.g.:

method test; unsafe;

Also, see the 'pinned' directive in Unsafe Code and Fixed Size Buffers for inline array support.

Generic Methods

See Generic Methods

Nested Methods

Nested methods are methods defined within another method, they're like an anonymous method in that they can access locals and parameters from within the same method, however they differ in that the changes made to the fields are reflected in the main method right away. Internally nested methods pass extra hidden parameters to the methods so it's not possible to create a delegate from a nested method.

method MyClass.Test(var s: string);
procedure AddSpace;
begin
  s := s + ' ';
end;

begin
  AddSpace;
end;

Anonymous Methods

See Anonymous_Methods_and_Delegates.

See Also


Oxygene-48.png

Area: Oxygene Language
Compiler version: Oxygene 5

Language GlossaryKeywordsTypesFAQHow To

Navigation
Areas
More
Toolbox