Am 31.10.2011 08:52, schrieb Andrew Pennebaker:
paycheck.pas:

unit Paycheck;
interface
type
     generic TArray<T> = array of T;
     generic TFn<T> = function() : T;
function GenInt () : TFn<integer>;
function GenBool() : TFn<boolean>;
function GenChar() : TFn<char>;
function GenArray(gen : TFn<T>) : TArray<T>;
function GenString() : TFn<string>;
{ ... }

I'm getting compiler errors for the generics.

$ fpc example.pas
Free Pascal Compiler version 2.7.1 [2011/10/31] for i386
Copyright (c) 1993-2011 by Florian Klaempfl and others
Target OS: Darwin for i386
Compiling example.pas
Compiling paycheck.pas
paycheck.pas(6,25) Error: Generics without specialization cannot be used
as a type for a variable
paycheck.pas(6,25) Fatal: Syntax error, ";" expected but "<" found
Fatal: Compilation aborted

Ok, you're experiencing two different problems here:

1. you are trying to do inline specializations in the functions "GenInt", "GenBool", "GenChar" and "GenString". This is not supported in mode "ObjFPC". You need to use "{$mode delphi}" instead (though I don't know whether this will indeed work in trunk).

2. Your function "GenArray" is a generic function. These are not yet supported by FPC (they are on my ToDo list though). Once they are implemented your declaration will need to look like this:

function GenArray<T>(gen: TFn<T>): TArray<T>;

And you will call them like this:

function Foo: Integer;
...

var
  intarray: TArray<Integer>;
begin
  intarray := GenArray<Integer>(@Foo);
end.

This does NOT work yet.

Regards,
Sven
_______________________________________________
fpc-pascal maillist  -  fpc-pascal@lists.freepascal.org
http://lists.freepascal.org/mailman/listinfo/fpc-pascal

Reply via email to