Parameters
This is a Language topic about Oxygene
Language Topics Introduction | Structured Overview | Grammar | Keywords | Functions
Method Parameters
Oxygene method parameters form part of the method's signature and need to be declared identically with the interface and implementation (thus supporting implicit method overloading).
Parameters can be of three types:
- simple - a single parameter with no default value
- list - several parameters of the same variable type
- default - a single parameter with a default value
All three types can be declared as const, var, out or the keyword can be omitted:
method Test1(a1: Word; const a2: String; var a3: Integer; out a4: Byte); method Test2(b1,b2: Word; const b3,b4: String; var b5,b6: Integer; out b7,b8: Byte); method Test3(c1: Word := 5; const c2: String := 'abc'; var c3: Integer := 8);
| |
|
|---|---|
| <omitted> | The parameter is passed by value. Any changes are temporary and are not passed back to the caller. |
| const | The value is fixed. |
| var | Changing its value will affect the caller. |
| out | Similar to var, but it does not need to be initialized by the caller. |
Normally, when calling a method that has var and/or out parameters, these keywords need to be specified, but there is a compiler option that allows the caller to omit var and out - see Project Options (Compatibility). The .NET runtime does not really support out params though, so when you define them in Oxygene (or C#) they are really just var parameters with that attribute. The compiler doesn't really treat the [Out] attribute as special, but just uses it when it finds the out keyword.
When declaring methods, Class Completion saves having to copy/paste from interface to implementation or vice versa and Class Navigation eases switching between the two sections.
Default Parameters
There are a couple of rules for using default parameters:
- they must follow all non-default parameters
- the default value should not be specified in the implementation header
Consider the following:
type ConsoleApp = class public class method Main; method Test3(c1: Word := 5; c2: String := 'abc'; c3: Integer := 8); end; implementation method ConsoleApp.Test3(c1:Word; c2:String; c3:Integer);
Compare the implementation header with that in the interface.
When calling a method that has default properties, you are only allowed to omit parameters working from the rightmost inwards, for example using the method declared above:
Test3; // c1=5 c2='abc' c3=8 Test3(9); // c1=9 c2='abc' c3=8 Test3(12,'xyz'); // c1=12 c2='xyz' c3=8 Test3(12,'xyz', 10); // c1=12 c2='xyz' c3=10 Test3('xxx',10); // error
Inline Array Parameters
The params keyword makes it easy to define methods that take a variable number of arguments. By closing the list of parameters with an array parameter prefixed with the params keyword, you can enable callers of your method to pass variable numbers of elements to method calls, which will automatically be converted into an array. This makes it easier to call the method, especially from languages like C#, where constructing an inline array is long and unwieldy.
method Format(aFormat: string: params values: array of Int32); //... Format('...', 1, 2, 3, 5, 27);
Untyped Parameters
Oxygene supports untyped parameters even without having to specify unsafe, e.g.:
interface procedure FreeAndNil(var obj); implementation procedure FreeAndNil(var obj); begin if Obj is IDisposable then IDisposable(Obj).Dispose; Obj := nil; end;
"Untyped" parameters are actually of type System.Object.
Attributes
When using attributes on parameters, you have to put them inline:
method Test([MyAttribute]A: Integer): [MyAttribute]Integer;