Another day, another compiler bug involving generics...

Another day, another compiler bug involving generics...

If I didn't know any better I'd almost feel special.

Here's the gist of it:

type
  TBidir = class
  strict private
    FValue1: T1;
    FValue2: T2;
  public
    constructor Create(const Value1: T1; const Value2: T2);

    function Inverse: TBidir;
  end;


{ TBidir }

constructor TBidir.Create(const Value1: T1; const Value2: T2);
begin
  inherited Create;

  FValue1 := Value1;
  FValue2 := Value2;
end;

function TBidir.Inverse: TBidir;
begin
  result := TBidir.Create(FValue2, FValue1); // E2010 Incompatible types: 'T2' and 'T1'
end;
https://quality.embarcadero.com/browse/RSP-16956

Comments

  1. Ouch, that's a weird one. Seems something with the parser or type lookup goes wrong there. If you make it both parameter show as Integer. If you make the second type something different than T1 it works.

    ReplyDelete
  2. The workaround I came up with is to create a descendant

    TInvBidir = class(TBidir)
    public
    constructor Create(const Value2: T2; const Value1: T1);
    end;

    and then have the constructor call the inherited TBidir constructor.

    Without the constructor it didn't work. I also returned an interface, so the TInvBidir had to explicitly implement the "inverse" interface as well.

    ReplyDelete
  3. I wonder what you are working on there... ;)

    ReplyDelete
  4. >The workaround I came up with is to create a descendant

    How are you going to explain to your child one day that they were conceived merely to work around a bug???

    ReplyDelete
  5. Interesting. Does it matter if you put in any constraints on T1 and T2?

    ReplyDelete
  6. Stefan Glienke Rather dull. Just needed a simple bidirectional map, with no extra dependency.

    ReplyDelete
  7. Lars Fosdal I tried just juggling around with type aliases but that didn't work. Didn't try constraints yet... will do just to see. I needed this unconstrained tho.

    ReplyDelete
  8. Joseph Mitzen had to constrain myself on the bus here not to LOL :)

    ReplyDelete
  9. That's the joys of Subscription (ransom ware) software. EMBT doesn't have to do or fix anything - yet they still receive their monthly subscriptions. In the good old days software companies had to actually improve the product before somebody would upgrade.

    ReplyDelete

Post a Comment