2

suppose i have this definitions:

TMyClass1 = class
end;

TMyClass2 = class
end;

IModel<T : class> = interface
  ['{E8262D6C-DCAB-46AC-822E-EC369CF734F8}']
  function List() : TObjectList<T>;
end;

IPresenter<T : class> = interface
  ['{98FB7751-D75A-4C51-B55A-0E5FE68BE213}']
  function Retrieve() : TObjectList<T>;
end;

IView<T : class> = interface
  ['{59384CD6-30D6-4BD8-AB3D-7FCF4D1A8618}']
  procedure AssignPresenter(APresenter : IPresenter<T>);
end;

TModel<T : class> = class(TInterfacedObject, IModel<T>)
public
  function List() : TObjectList<T>; virtual; abstract;
end;

TPresenter<T : class> = class(TInterfacedObject, IPresenter<T>)
strict private
  { Private declarations }
  FModel : IModel<T>;
  FView : IView<T>;
public
  constructor Create(AView : IView<T>);
  function Retrieve() : TObjectList<T>; virtual; abstract;
end;

TModelClass1 = class(TModel<TMyClass1>);

TPresenterClass1 = class(TPresenter<TMyClass1>);

TModelClass2 = class(TModel<TMyClass2>);

TPresenterClass2 = class(TPresenter<TMyClass2>);

and i have this form that implements some of the things i defined:

TForm1 = class(TForm, IView<TMyClass1>, IView<TMyClass2>)
  procedure FormCreate(Sender: TObject);
private
  { Private declarations }
  FPresenter1 : IPresenter<TMyClass1>;
  FPresenter2 : IPresenter<TMyClass2>;
  procedure AssignPresenter(APresenter : IPresenter<TMyClass1>); overload;
  procedure AssignPresenter(APresenter : IPresenter<TMyClass2>); overload;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 TPresenterClass1.Create((Self as IView<TMyClass1>));
 TPresenterClass2.Create((Self as IView<TMyClass2>));
end;

procedure TForm1.AssignPresenter(APresenter: IPresenter<TMyClass1>);
begin
  Self.FPresenter1 := APresenter;
end;

procedure TForm1.AssignPresenter(APresenter: IPresenter<TMyClass2>);
begin
  Self.FPresenter2 := APresenter;
end;

so the problem here is that delphi can't figure out what method to call, in this example, only the AssignPresenter(APresenter: IPresenter<TMyClass2>) is called in both cases, so probably im missing something here but i can't figure out atm.

thx in advance.

0

3 Answers 3

3

This is probably a duplicate but I cannot find it right now.

The problem is that the as operator for interfaces is not very compatible with generics. The as operator relies on the interface GUID. The interface is found by querying for an interface with matching GUID. And GUIDs don't fit at all well with generic instantiation.

Now let's look at your code.

TPresenterClass1.Create((Self as IView<TMyClass1>));
TPresenterClass2.Create((Self as IView<TMyClass2>));

The problem is that IView<TMyClass1> and IView<TMyClass2> have the same GUID:

type
  IView<T : class> = interface
    ['{59384CD6-30D6-4BD8-AB3D-7FCF4D1A8618}']
    procedure AssignPresenter(APresenter : IPresenter<T>);
  end;

So both IView<TMyClass1> and IView<TMyClass2> share the same GUID, and when you query using as, the same interface will be returned irrespective of whether or not you asked for IView<TMyClass1> or IView<TMyClass2>.

So, the bottom line here is that as is rendered next to useless with generic interfaces as soon as an object implements ISomeInterface<T> twice with different T.

Embarcadero really should implement as in a manner that supports generics. I wouldn't hold your breath though.

You will need to find a different way to solve your problem.

1
  • @EProgrammer I found that one but it's a little different. It's about there being no GUID. This one's about generic instantiations and GUIDs. Commented Feb 12, 2015 at 20:00
2

No need to use as when you have the object implementing the interface - just write:

procedure TForm1.FormCreate(Sender: TObject);
begin
  TPresenterClass1.Create(Self);
  TPresenterClass2.Create(Self);
end;

The compiler figures this out properly. When you are using as it does a Supports call which fails for the reason explained by David already.

0
0

As an addition to David Heffernan's answer.

One way you could work around the problem would be to give a unique GUID to all the generic declarations you use:

IViewMyclass1 = interface(IView<TMyClass1>)
['{1A0F941F-BAB1-4723-A6C1-27036DF5D344}']
end;

IViewMyclass2 = interface(IView<TMyClass2>)
['{0C61A23A-DC50-43B0-97C9-8B0013DDC193}']
end;

Redefine the view's declaration.

TForm4 = class(TForm, IViewMyclass1, IViewMyclass2)

procedure TForm1.FormCreate(Sender: TObject);
begin
 TPresenterClass1.Create((Self as IViewMyclass1));
 TPresenterClass2.Create((Self as IViewMyclass2));
end;

And then the right overload is called.

Disclaimers:

  • This is the result of On-The-Fly testing (XE4). Reliability is unknown.
  • I'm aware this isn't very practical.
  • It's probably not something that should be extensively used as it isn't obvious where the code would fail.
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.