Sign in Join

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

Mailing lists / fpc-pascal / 2017-10

Generic<T> compiler error

11 messages · 5 people ·

Reply by email Subscribe to this list Your subscription

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

Marcos Douglas B. Santos

Hi,

In Pascal we can do this:

type
  TXStream = TStream;

Now, TXStream is just an alias to TStream.

How do the same with generic classes, using mode delphi?

  TBar<T> = TFoo<T>;
Compile Project, Target: C:\temp\project1.exe: Exit code 1, Errors: 4
project1.lpr(25,18) Error: Identifier not found "T"
project1.lpr(25,19) Error: Type identifier expected
project1.lpr(25,20) Error: Error in type definition
project1.lpr(27,1) Error: This type cannot be a generic

My ENV is:
Lazarus 1.9.0 r56056M
FPC 3.1.1 r37505
i386-win32-win32/win64

Please, see an example below:

=== BEGIN ===

program Project1;
{$mode delphi}
uses
  Classes;
type
  IFoo<T> = interface
  ['{5CA4AEC4-3875-4639-9975-AC2C201E0A98}']
    function Value: T;
  end;
  TFoo<T> = class(TInterfacedObject, IFoo<T>)
  private
    FValue: T;
  public
    constructor Create(Value: T);
    class function New(Value: T): IFoo<T>;
    function Value: T;
  end;
  TXStream = TStream;  // <<<< OK
  TBar<T> = TFoo<T>; // <<<< how solve this?
constructor TFoo<T>.Create(Value: T);
begin
  inherited Create;
  FValue := Value;
end;
class function TFoo<T>.New(Value: T): IFoo<T>;
begin
  Result := Create(Value);
end;
function TFoo<T>.Value: T;
begin
  Result := FValue;
end;
begin
  Writeln(TFoo<Integer>.New(1).Value);
  Readln;
end.

=== END ===

Reply by email

Ryan Joseph
to Marcos Douglas B. Santos
On Oct 23, 2017, at 9:14 AM, Marcos Douglas B. Santos wrote: How do the same with generic classes, using mode delphi?

I think you need to specialize then alias the specialize type. Why do you want to make an alias for an untyped generic anyways?

Regards,
Ryan Joseph

Reply by email

Sven Barth

Am 23.10.2017 04:14 schrieb "Marcos Douglas B. Santos" :

Hi,

In Pascal we can do this:

type
  TXStream = TStream;

Now, TXStream is just an alias to TStream.

How do the same with generic classes, using mode delphi?

  TBar<T> = TFoo<T>;

Type aliases are currently not supported in mode Delphi. In mode ObjFPC they might work, but I'm not sure about that either...

That said I also don't see the use of type aliases for generics...

Regards,
Sven

Reply by email

Marco van de Voort
to Sven Barth

In our previous episode, Sven Barth via fpc-pascal said:

Show quoted text 5 lines
Type aliases are currently not supported in mode Delphi. In mode ObjFPC they might work, but I'm not sure about that either... That said I also don't see the use of type aliases for generics...

How about a simple

type  tttt<x> = anotherunit.tttt<x>;

?

That is often what I use type aliases for, aliasing code that has been factored out of an unit back in.

Reply by email

Sven Barth
to Marco van de Voort

Am 23.10.2017 08:29 schrieb "Marco van de Voort" :

In our previous episode, Sven Barth via fpc-pascal said:

Show quoted text 5 lines
Type aliases are currently not supported in mode Delphi. In mode ObjFPC they might work, but I'm not sure about that either... That said I also don't see the use of type aliases for generics...

How about a simple

type  tttt<x> = anotherunit.tttt<x>;

?

That is often what I use type aliases for, aliasing code that has been factored out of an unit back in.

Okay, that's an acceptable use case ^^'

Regards,
Sven

Reply by email

Marcos Douglas B. Santos
to Marco van de Voort: In our previous episode, Sven Barth via fpc-pascal said: How about a simple type tttt<x>…
Marco van de Voort wrote 16 lines
In our previous episode, Sven Barth via fpc-pascal said: > > Type aliases are currently not supported in mode Delphi. In mode ObjFPC > they might work, but I'm not sure about that either... > > That said I also don't see the use of type aliases for generics... How about a simple type tttt<x> = anotherunit.tttt<x>; ? That is often what I use type aliases for, aliasing code that has been factored out of an unit back in.

+1
That is I am talking about. Thanks.

Best regards,
Marcos Douglas

Reply by email

Marcos Douglas B. Santos
to Ryan Joseph: I think you need to specialize then alias the specialize type. Why do you want to make an…
Ryan Joseph wrote 7 lines
> On Oct 23, 2017, at 9:14 AM, Marcos Douglas B. Santos wrote: > > How do the same with generic classes, using mode delphi? I think you need to specialize then alias the specialize type. Why do you want to make an alias for an untyped generic anyways?

As Marco showed, to do this:

type  tttt<x> = anotherunit.tttt<x>;

Best regards,
Marcos Douglas

Reply by email

Marcos Douglas B. Santos
to Sven Barth: In our previous episode, Sven Barth via fpc-pascal said: How about a simple type tttt<x>…
Sven Barth wrote 21 lines
Am 23.10.2017 08:29 schrieb "Marco van de Voort" : In our previous episode, Sven Barth via fpc-pascal said: > > Type aliases are currently not supported in mode Delphi. In mode ObjFPC > they might work, but I'm not sure about that either... > > That said I also don't see the use of type aliases for generics... How about a simple type tttt<x> = anotherunit.tttt<x>; ? That is often what I use type aliases for, aliasing code that has been factored out of an unit back in. Okay, that's an acceptable use case ^^'

So, can we have this in the next version? :)

Best regards,
Marcos Douglas

Reply by email

Sven Barth
to Marcos Douglas B. Santos

Am 23.10.2017 12:55 schrieb "Marcos Douglas B. Santos" :

Sven Barth wrote 21 lines
Am 23.10.2017 08:29 schrieb "Marco van de Voort" : In our previous episode, Sven Barth via fpc-pascal said: > > Type aliases are currently not supported in mode Delphi. In mode ObjFPC > they might work, but I'm not sure about that either... > > That said I also don't see the use of type aliases for generics... How about a simple type tttt<x> = anotherunit.tttt<x>; ? That is often what I use type aliases for, aliasing code that has been factored out of an unit back in. Okay, that's an acceptable use case ^^'

So, can we have this in the next version? :)

Don't know. Only time will tell.

Regards,
Sven

Reply by email

Sven Barth
to Maciej Izak

Am 23.10.2017 14:37 schrieb "Maciej Izak" :

2017-10-23 14:33 GMT+02:00 Sven Barth via fpc-pascal <

: Don't know. Only time will tell.

Did we have new team member? I would like to meet him! ;)

Sadly time isn't around that often. :P (and why do you assume that it's a he? :P)

Regards,
Sven

Reply by email