A r t i c l e s
Navigation

Note: This site is
a bit older, personal views
may have changed.

M a i n P a g e

D i r e c t o r y

Launching a Function When Declared as Type



Problem:

This below will cause a compiler error, because the compiler thinks that you are trying to 
make a boolean a function, which is impossible. The compiler doesn't know you are 
trying to acutally launch the function since the function is a type.

program Project1;

{$mode objfpc}{$H+}

type
 TIsItTrue = function: boolean;
 
var
  MyBoolean: Boolean;
  IsItTrue: TIsItTrue;

begin
 MyBoolean:= IsItTrue; //<---COMPILER ERROR
 readln;
end.

Solution:

This syntax below will solve the problem and launch your function for you.

program Project1;

{$mode objfpc}{$H+}

type
 TIsItTrue = function: boolean;
 
var
  MyBoolean: Boolean;
  IsItTrue: TIsItTrue;

begin
 MyBoolean:= IsItTrue();  //note brackets
 readln;
end.

The brackets act as a syntax hack, to let the compiler know that you are launching
the function NOT that you are trying to declare the boolean as a function. --L505
However, in {$mode delphi} the compiler seems to eat it up fine without the brackets.

About
This site is about programming and other things.
_ _ _