Sign in Join

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

Mailing lists / fpc-devel / 2012-05

Generics are still broken

9 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

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

Anthony Walter

Generics still have a lot of problems. I know they've been in the works for years but they still fail easily. Consider the Delphi mode below, I get "Error: Generics without specialization cannot be used as a type for a variable"

I could easily write up a whole bunch of test cases were generic still fail, both in Fpc and Delphi modes. If someone is considering working on the compiler to fix the problems with generics I can happily send them a bunch of test cases where the compiler currently fails.

{$mode delphi}

{ TEnumerator<T> }

type
  TEnumerator<T> = class abstract
  protected
    function DoGetCurrent: T; virtual; abstract;
    function DoMoveNext: Boolean; virtual; abstract;
  public
    property Current: T read DoGetCurrent;
    function MoveNext: Boolean;
  end;

{ TEnumerable<T> }

  TEnumerable<T> = class abstract
  protected
    function DoGetEnumerator: TEnumerator<T>; virtual; abstract; // Error:
Generics without specialization cannot be used as a type for a variable
  public
    function GetEnumerator: TEnumerator<T>;
  end;

Reply by email

JC Chu
to Anthony Walter

It will compile correctly with the trunk version.

Generics support has been greatly improved in the trunk version; you may want to give it a try.

Anthony Walter wrote 39 lines
Generics still have a lot of problems. I know they've been in the works for years but they still fail easily. Consider the Delphi mode below, I get "Error: Generics without specialization cannot be used as a type for a variable" I could easily write up a whole bunch of test cases were generic still fail, both in Fpc and Delphi modes. If someone is considering working on the compiler to fix the problems with generics I can happily send them a bunch of test cases where the compiler currently fails. {$mode delphi} { TEnumerator<T> } type TEnumerator<T> = class abstract protected function DoGetCurrent: T; virtual; abstract; function DoMoveNext: Boolean; virtual; abstract; public property Current: T read DoGetCurrent; function MoveNext: Boolean; end; { TEnumerable<T> } TEnumerable<T> = class abstract protected function DoGetEnumerator: TEnumerator<T>; virtual; abstract; // Error: Generics without specialization cannot be used as a type for a variable public function GetEnumerator: TEnumerator<T>; end; _______________________________________________ fpc-devel maillist - http://lists.freepascal.org/mailman/listinfo/fpc-devel
Best regards, JC Chu

Reply by email

Anthony Walter
to JC Chu: It will compile correctly with the trunk version. Generics support has been greatly…

Okay with the latest trunk version I tried compiling my generic classes. It seems there is a problem with fpc generics in that you cannot use a generic as a type parameter. e.g type TDictionary<K,V> =
class(TEnumerable<TPair<K,V>>) ...

Given:

type
  TCollection<T> = class
  end;
  TPair<K,V> = class
    Key: K;
    Value: V;
  end;
// Test
var
  TestA: TCollection<TPair<String,Integer>>; // Fatal: Syntax error, ","
expected but "<" found
  TestB: TCollection<String>; // Okay

The same problem occurs with classes

type
  TDictionaryA<K,V> = class(TCollection<TPair<K,V>>)  // Fatal: Syntax
error, "," expected but "<" found
  end;
  TDictionaryB<K,V> = class(TCollection<Integer>)  // Okay
  end;

Reply by email

Sven Barth
to Anthony Walter
Anthony Walter wrote
Okay with the latest trunk version I tried compiling my generic classes. It seems there is a problem with fpc generics in that you cannot use a generic as a type parameter. e.g type TDictionary<K,V> = class(TEnumerable<TPair<K,V>>) ...

Yes, this is a known problem and already on my todo list (like most other generic bugs...)

Regards,
Sven

Reply by email

Anthony Walter
to Sven Barth

Thanks Sven. Overall generics in Fpc are very much improved. So good work! Your work is much appreciated!

I also experienced some different problems with classes which implement generic interfaces where the compiler thinks that method aren't implement.

On my blog I have a page where I describe multicast events through delegates source: http://www.codebot.org/delphi/?doc=9568

TDelegateImpl<T> = class(TInterfacedObject, IDelegate<T>)

Fpc gives me some problems with the class above specifically. All the sources are on that page and they currently work with Delphi. I can probably boil the problem into a smaller test case.

If you want some help let me know what I can do.

Sven Barth wrote 19 lines
On 11.05.2012 20:10, Anthony Walter wrote: > Okay with the latest trunk version I tried compiling my generic classes. > It seems there is a problem with fpc generics in that you cannot use a > generic as a type parameter. e.g type TDictionary<K,V> = > class(TEnumerable<TPair<K,V>>) ... > Yes, this is a known problem and already on my todo list (like most other generic bugs...) Regards, Sven ______________________________**_________________ fpc-devel maillist - http://lists.freepascal.org/**mailman/listinfo/fpc-devel<http://lists.freepascal.org/mailman/listinfo/fpc-devel>

Reply by email

Sven Barth
to Anthony Walter
Anthony Walter wrote 16 lines
Thanks Sven. Overall generics in Fpc are very much improved. So good work! Your work is much appreciated! I also experienced some different problems with classes which implement generic interfaces where the compiler thinks that method aren't implement. On my blog I have a page where I describe multicast events through delegates source: http://www.codebot.org/delphi/?doc=9568 TDelegateImpl<T> = class(TInterfacedObject, IDelegate<T>) Fpc gives me some problems with the class above specifically. All the sources are on that page and they currently work with Delphi. I can probably boil the problem into a smaller test case. If you want some help let me know what I can do.

If you want to report bugs it would be nice if you

1. check whether it was already reported. Most generic bugs have the tag "generics", which you can reach here:
http://bugs.freepascal.org/tag_view_page.php?tag_id=3 (click on "attached issues" in the upper right to see a list of all generics bugs (opened or closed) that are currently in Mantis)
2. add the tag "generics" to new bug reports, so that I can find them quickly.

This would help already if you discover bugs.

Regards,
Sven

Reply by email