Var (keyword)

From The Oxygene Language Wiki

Jump to:navigation, search

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



var is used to define and optionally initialize a variable. In classic Pascal and Delphi, var was actually a separate section in the beginning of a method. While this still works in Delphi Prism, you can use var inline with your code.

Old style var section

method DoSomething;
var
  someInt: Integer;
  someBoolean: Boolean;
begin
  //some code
end;

var as inline statement

method DoSomething;
begin
  var someInt: Integer;
  //some code
  var someBoolean: Boolean;
  //some more code
  var AnswerToLifeTheUniverseAndEverything: Integer := 42;
end;

the var keyword can also be used in combination with Type Inference

method DoSomething;
begin
  var someInt := SomeMethodThatReturnsAnInteger();
  //some code
  var someBoolean := SomeMethodThatReturnsABoolean();
  //some more code
end;

In the above code the two variables are still strongly typed, but there type is inferred from the method call return type.

Also var can be used to declare method parameters:

method Test1(a1: Word; var a2: Integer; var a4: Byte);

Changing value of this parameters will affect the caller.


Oxygene-48.png

Area: Oxygene Language
Compiler version: Oxygene 5

Language GlossaryKeywordsTypesFAQHow To

Navigation
Areas
More
Toolbox