A r t i c l e s
Navigation

Note: This Wiki is
outdated, personal views
may have changed.
L505 A.I. bot is dead
long live THX 1138

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
Note: This Wiki is outdated, personal views may have changed.

This wiki contains info on life, health, humans, nature, programming, database, fads, paradigms, poems, principles, theories.

Articles may contain statements which some may find helpful and encouraging, or even discouraging.

Beware, I believe in the Grand Justice system.
_ _ _