Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-10

Generic specialization using $mode objfpc

9 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

silvioprog

Hello,

How to use this code below in mode objfpc?:

  TMyGeneric<T> = class(TObject)
  private
    FValue: T;
  public
    property Value: T read FValue write FValue;
  end;

...

var
  VMyGenericStr: TMyGeneric<Integer>;
  VMyGenericInt: TMyGeneric<string>;
begin
  VMyGenericStr := TMyGeneric<Integer>.Create;
  VMyGenericStr.Value := 2014;
  VMyGenericStr.Free;
  VMyGenericInt := TMyGeneric<string>.Create;
  VMyGenericInt.Value := 'Delphi Generics';
  VMyGenericInt.Free;
end;

When I try:

  generic TMyGeneric<T> = class(TObject)
  private
    FValue: T;
  public
    property Value: T read FValue write FValue;
  end;

...

var
  VMyGenericStr: specialize TMyGeneric<Integer>;
  VMyGenericInt: specialize TMyGeneric<string>;
begin
  VMyGenericStr := TMyGeneric<Integer>.Create;
  VMyGenericStr.Value := 2014;
  VMyGenericStr.Free;
  VMyGenericInt := TMyGeneric<string>.Create;
  VMyGenericInt.Value := 'Delphi Generics';
  VMyGenericInt.Free;
end;

I got:

Error: Illegal expression
Error: Operator is not overloaded: "Class Of TMyGeneric$1" < "LongInt" Fatal: Syntax error, ";" expected but "identifier CREATE" found

Thank you!

ps: FPC from trunk.

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

Kiên Nguyễn Tiến Trung
to silvioprog

AFAIK, you must create a new type:

type
    TMyGenericInt = specialize TMyGeneric<Integer>;
var
    VMyGenericInt: TMyGenericInt;
begin
    VMyGenericInt:= TMyGenericInt.Create;
    VMyGenericInt.Value := 2014;
    VMyGenericInt.Free;
end.

2014-10-31 2:14 GMT+07:00 silvioprog :

Show quoted text 68 lines
Hello, How to use this code below in mode objfpc?: TMyGeneric<T> = class(TObject) private FValue: T; public property Value: T read FValue write FValue; end; ... var VMyGenericStr: TMyGeneric<Integer>; VMyGenericInt: TMyGeneric<string>; begin VMyGenericStr := TMyGeneric<Integer>.Create; VMyGenericStr.Value := 2014; VMyGenericStr.Free; VMyGenericInt := TMyGeneric<string>.Create; VMyGenericInt.Value := 'Delphi Generics'; VMyGenericInt.Free; end; When I try: generic TMyGeneric<T> = class(TObject) private FValue: T; public property Value: T read FValue write FValue; end; ... var VMyGenericStr: specialize TMyGeneric<Integer>; VMyGenericInt: specialize TMyGeneric<string>; begin VMyGenericStr := TMyGeneric<Integer>.Create; VMyGenericStr.Value := 2014; VMyGenericStr.Free; VMyGenericInt := TMyGeneric<string>.Create; VMyGenericInt.Value := 'Delphi Generics'; VMyGenericInt.Free; end; I got: Error: Illegal expression Error: Operator is not overloaded: "Class Of TMyGeneric$1" < "LongInt" Fatal: Syntax error, ";" expected but "identifier CREATE" found Thank you! ps: FPC from trunk. -- Silvio Clécio My public projects - github.com/silvioprog _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal

Reply by email

silvioprog
to Kiên Nguyễn Tiến Trung
Kiên Nguyễn Tiến Trung wrote 13 lines
AFAIK, you must create a new type: type TMyGenericInt = specialize TMyGeneric<Integer>; var VMyGenericInt: TMyGenericInt; begin VMyGenericInt:= TMyGenericInt.Create; VMyGenericInt.Value := 2014; VMyGenericInt.Free; end.

Hm... so I'll choose the mode delphi in my new projects to type less code hehe...

Thanks guy! =)

Silvio Clécio My public projects - github.com/silvioprog

Reply by email

leledumbo
to silvioprog
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.

View this message in context: http://free-pascal-general.1045716.n5.nabble.com/Generic-specialization-using-mode-objfpc-tp5720409p5720429.html Sent from the Free Pascal - General mailing list .

Reply by email

silvioprog
to leledumbo
leledumbo wrote 9 lines
> 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

Reply by email

silvioprog
to silvioprog
silvioprog wrote 82 lines
On Sat, Nov 1, 2014 at 1:54 AM, leledumbo 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

Reply by email

Sven Barth
to silvioprog
silvioprog wrote 28 lines
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. =)

Yes, since the ObjFPC dialect was developed without type overloading in mind (in Delphi (and FPC's mode Delphi) you can have TSingleton<T> and TSingleton<T, S> in the same unit) it was decided that it is not necessary to add the <T> at the definiton of the method as well.

Regards,
Sven

Reply by email

silvioprog
to Sven Barth
Sven Barth wrote 36 lines
On 11.11.2014 22:48, silvioprog wrote: > 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. =) > Yes, since the ObjFPC dialect was developed without type overloading in mind (in Delphi (and FPC's mode Delphi) you can have TSingleton<T> and TSingleton<T, S> in the same unit) it was decided that it is not necessary to add the <T> at the definiton of the method as well.

Awesome. =)

Silvio Clécio My public projects - github.com/silvioprog

Reply by email