Delphi's generic constraint is so much limiting.

Delphi's generic constraint is so much limiting.

Please look at the piece of code below:
type
TGenericTesting = class
end;

When I try to declare an variable of this type:
var
LInvalidGeneric:TGenericTesting;

I got the following error message:
[dcc64 Error] E2511 Type parameter 'T' must be a class type.

Well, I know generic parameter T is limited to class types but this error is unexpected because an interface can also be considered an abstract class and that assumption is true in many other languages as C++, C#, VB.NET, F#, etc.
In C# language for example:

public class GenericTesting where T: class
{
}

The generic parameter T may accept a class as well as an interface. That allows fine-grained generic constraint. If I wanted to limit the generic parameter T only for interfaces (abstract classes) I could not do that in the current implementation of Delphi. This little change in the compiler to accept interface types as abstract class types will make this possible and it will be also consistent with other programming languages.

This issue (RSP-10267) is open yet in Quality Portal, please help me voting in https://quality.embarcadero.com/browse/RSP-10267.

Thanks :D

"For a Delphi more fun."
http://VB.NET

Comments

  1. In C# the class constraint means "a reference type". In Delphi it means "a class". In C# that is possible because classes and interfaces are not handled much differently. In Delphi they are.
    The entire generic constraints in Delphi are broken and inconsistent. Like its class for classes but for interfaces I have to write IInterface or IUnknown, why not interface there as well? Or why can't I write TObject as constraint?

    ReplyDelete

Post a Comment