[fpc-pascal] Generic specialization using $mode objfpc

silvioprog silvioprog at gmail.com
Tue Nov 11 22:48:49 CET 2014


On Tue, Nov 11, 2014 at 5:34 PM, silvioprog <silvioprog at gmail.com> wrote:

> 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.
>
> =(
>

Oh, sorry. Just:

unit Core.Singleton;

{$mode objfpc}{$H+}

interface

type

  { TSingleton }

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

implementation

{ TSingleton }

class function TSingleton.GetInstance: T;
begin
end;

end.

And compiled like a charm. =)

-- 
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/af46a220/attachment.html>


More information about the fpc-pascal mailing list