[fpc-pascal] Generic specialization using $mode objfpc

silvioprog silvioprog at gmail.com
Tue Nov 11 21:34:54 CET 2014


On Sat, Nov 1, 2014 at 1:54 AM, leledumbo <leledumbo_cool at yahoo.co.id>
wrote:

> > Hm... so I'll choose the mode delphi in my new projects to type less code
> hehe...
>
> You actually ended up typing much less using objfpc style generics. Think
> of
> how many angle brackets you would need to type in Delphi mode? And how many
> if the specialized type changes? ;)
> Delphi generics can actually do the same (create a new type first), but
> objfpc enforces this.


Yes, but I had some problems to understand the generic sintax in objfpc (it
is not documented). =(

For example, how to compile this code using objfpc?:

unit Core.Singleton;

{$mode delphi}

interface

type

  { TSingleton }

  TSingleton<T: class, constructor> = class(TObject)
  public
    class function GetInstance: T;
  end;

implementation

{ TSingleton }

class function TSingleton<T>.GetInstance: T;
begin
  { TODO: implement! }
end;

end.

If I try to use mode objfpc:

unit Core.Singleton;

{$mode objfpc}{$H+}

interface

type

  { TSingleton }

  generic TSingleton<T: class, constructor> = class(TObject)
  public
    class function GetInstance: T;
  end;

implementation

{ TSingleton }

class function TSingleton<T>.GetInstance: T;
begin
  { TODO: implement! }
end;

end.

The error is:

Compile Project, Target: project1.exe: Exit code 1, Errors: 1
Core.Singleton.pas(11,32) Error: Illegal expression.

=(

--
Silvio Clécio
My public projects - github.com/silvioprog
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20141111/2b3163eb/attachment.html>


More information about the fpc-pascal mailing list