[fpc-pascal] Interface performance

Ryan Joseph ryan at thealchemistguild.com
Sat Nov 12 04:14:57 CET 2016


> On Nov 12, 2016, at 3:22 AM, Jonas Maebe <jonas.maebe at elis.ugent.be> wrote:
> 
> You're passing a class, not an object. Passing a class that implements an interface to a method that expects that interface should work fine. Converting a class instance to an interface that it implements is trivial and always works, it's doing the opposite that is hard and that requires specific support in the interface.

Here’s a better example of where this breaks. Yes casting does work to an extent but breaks eventually down the chain when I lose the reference. Is there anyway to get around this? Maybe I could store the reference from Supports inside TMyObjectClass so I don’t need to call Supports again?

type
	IMyInterface = interface
	  procedure DoSomething; 
		function GetObject: TObject;
	 end;  

type
	TMyObjectClass = class(IMyInterface)
	  	procedure DoSomething;
			function GetObject: TObject;
	end; 

var Obj: TMyObjectClass
ObjInt: IMyInterface;

Obj := TMyObject.Create;
ObjInt := Obj; // this works fine, casting to an interface

// Unit A only knows about IMyInterface so we pass
// the class cast as an interface (not sure what to call this)
DomeSomethingInUnitA(ObjInt);

// Unit A
var Obj: TObject;
Arr: TArray; // collection class the stores TObject

Arr := TArray.Create;
Obj := ObjInt.GetObject;
Arr.AddObject(Obj);

[do some stuff]

// here the problems start because TArray always returns TObject
// and I can’t cast TObject to IMyInterface even though the object
// stored in the array does in fact implement IMyInterface

ObjInt := IMyInterface(Arr.GetObject(0)); // error! but I need to get a reference to IMyInterface from the array

// I have to use “as” or Supports here and it defeats the purpose
ObjInt := Arr.GetObject(0) as IMyInterface;


Regards,
	Ryan Joseph




More information about the fpc-pascal mailing list