Generic records (from my record wishlist).

Generic records (from my record wishlist).

I wish the following could be possible:

type
TRecA = record; abstract; // defines contract
procedure MethodA; // no implementation provided;
end;

TRecB = record implements TRecA;
FieldB: Integer;
procedure MethodA; // contract is implemented here;
end;

TRecC = record
FieldC: Integer;
FRecA: TRecA;
procedure MethodC; // calls FRecA.MethodA
end;

TRecD = TRecC;

Comments

  1. This feature does not need record inheritance nor abstract/virtual methods on records. It just needs a more advanced constraint system where you can specify that your T in TRec needs to have some MethodA.

    For C# they are working on this for quite a while - filed under different names but this is one: https://github.com/dotnet/csharplang/issues/164

    ReplyDelete
  2. Yes this feature does need anything except the feature itself; and I want the feature because currently I rewrite manually many times MethodC methods instead of writing generic MethodC once and then simply write

    TRecD = TRecC;
    TRecK = TRecC;
    TRecL = TRecC;

    etc,

    where TRecB, TRecE and TRecF implement the contract TRecA

    ReplyDelete
  3. Understood, I just wanted to emphasize that record inheritance and more so abstract/virtual methods would be kind of a bad approach to solve this problem as it would a) only solve this for records and b) open an entirely different can of worms.

    IMO to properly implement this and other similar issues with constraining of generics without making them C++ templates we need something similar to the C# approach and the compiler then evaluates if types match the constraint and produces the proper code to bind.

    Scala solves this by using traits: see https://apiumhub.com/tech-blog-barcelona/scala-generics-generalized-type-constraints/ (not sure though traits can be used on structs)

    ReplyDelete

Post a Comment