Quantcast
Channel: Planet Object Pascal
Viewing all articles
Browse latest Browse all 1725

Free Pascal Answers: New language additions in Free Pascal

$
0
0

These are many new improvements of Object Pascal programming language in Free Pascal compiler. I don’t know how much new are they, but at least they are new since Delphi 7 language. I haven’t used any of these features before (until now) because I wasn’t know about them.

1. For .. in loop:

This can be used to loop in string characters:


procedure ForInLoop(aStr: string);
var
  ch: Char;
begin
  for ch in aStr do
    WriteLn(ch);
end;

or through set items:


procedure ForInSetLoop;
var
  s: set of 1 .. 100;
  i: Integer;
begin
  s:= [1, 3, 7];
  for i in s do
    Writeln(i);
end;

2. += operator

This new operator can be used to concatenate strings and add values to numbers:


var
  aName: string;
begin
  aName:= 'Free';
  aName += ' Pascal';
  Writeln(aName); // Free Pascal
end;

For numbers:


x:= 10;
x+= 15;
Writeln(x); // 25

3. Properties without OOP

Now you can define a property in a structured code:


var
  x: Integer;

procedure SetX(aX: Integer);
begin
  x:= ax;
end;

function GetX: Integer;
begin
  Result:= x;
end;

property MyX: Integer read GetX write SetX;

// Main program
begin
  MyX:= 170;
  Writeln(MyX);

end.

4. Bit packed record

You can define a record of bits, and display it as byte:

type
  tbit = 0..1;

  tBitsByte = bitpacked record
    bit0   : tbit;
    bit1   : tbit;
    bit2   : tbit;
    bit3   : tbit;
    bit4   : tbit;
    bit5   : tbit;
    bit6   : tbit;
    bit7   : tbit;
  end;

var
  aByte: tBitsByte;
begin
  aByte.bit0:= 1;
  aByte.bit1:= 0;
  aByte.bit2:= 1;
  Writeln(Byte(aByte)); // 5

5. Sealed class

You can prevent inheriting from a class by adding the keyword sealed:

TMyClass = class sealed
  private
    fValue: Integer;
  public
    constructor Create(aValue: Integer);
    destructor destroy; override;
    function GetValue: Integer;
end;

I couldn’t find a good example right now to explain why we need to do this, but in the future I may get one.

6. Class methods and variables

You can declare methods and variables that can be used by class name before object instantiation the same like Java static methods as in this example:


TMyClass = class sealed
  private
    class var fValue: Integer;
  public
    constructor Create(aValue: Integer);
    class function GetValue: Integer;
    class procedure SetValue(aValue: Integer);
end;

constructor TMyClass.Create(aValue: Integer);
begin
  inherited Create;
  fValue:= aValue;
end;

class function TMyClass.GetValue: Integer;
begin
  Result:= fValue;
end;

class procedure TMyClass.SetValue(aValue: Integer);
begin
  fValue:= aValue;
end;

// Main code

begin
  TMyClass.SetValue(900);
  Writeln(TMyClass.GetValue);
end.

7. case of String


case aName of
'Free Pascal': Writeln('Lazarus IDE');
'C++': Writeln('CodeBlocks IDE');
end;

 

If you know any new useful additions please let me now to write it here.

References:

http://lazarus.freepascal.org/index.php/topic,19107.0.html

http://www.freepascal.org/docs-html/ref/refse24.html#x56-630004.6

http://edn.embarcadero.com/article/34324



Viewing all articles
Browse latest Browse all 1725

Trending Articles