Casting an Interface Reference to the Implementing Class in Delphi 2010
Not all the new features in Delphi 2010 are big. In fact, the team have spent a lot of time implementing many small features, fixes and tweaks. Some of these may not seem to amount to much individually, but they not only add up to significant impact, but greatly add to the polish of the product. I expect this will be one of those releases that keeps serving up little delights and surprises for a long time.
One of the features in Delphi 2010 that I expect will spawn much debate is the ability to cast an interface reference back to the type of class that implements it.
Let’s say we have an Interface, IMyInterface, and a class that implements that Interface, TMyClass:
IMyInterface = interface ['{7AD04980-5869-4C93-AC29-C26134511554}'] procedure Foo; end; TMyClass = class(TInterfacedObject, IMyInterface) procedure Foo; procedure Bar; end; |
Further, let’s say we’ve been passed a variable of type IMyInterface. What happens if we want to invoke Bar? Attempting to simply cast the interface reference to a TMyClass would result in a compiler error.
The most common solution to this I’ve seen is to have a method in the interface that returns the class type, but this kind of defeats much of the value of the interface in the first place, tying the interface to a specific implementation. Further, it’s ugly as a hatful.
Well, such hacks are no longer required.
In Delphi 2010, you can now use the is operator to test to see if an interface type is implemented by a specific class, and if so, cast it to that type and reference any non-interface methods, properties, etc.
Further, the as operator will throw an EInvalidCast exception if you attempt to cast an interface reference to a class type that it was not actually extracted from. The hard cast will return nil in the same circumstances. (Thanks Allen for the correction!)
So this code now runs happily:
if MyInterface is TMyClass then TMyClass(MyInterface).Bar; |
This needs to be used with some intelligence, of course. For example, the normal warnings about storing a reference-counted interface and a non-reference counted object reference to the same instance still apply.
Related posts:
[...] Malcolm Groves shows off YACLF* in Delphi 2010. [...]
[...] that in mind, I thought it very much a Good Thing when Malcolm Groves reported how yet another COM-related quirk — namely, the inability to cast from an interface to an [...]
[...] mentioned earlier that a lot of work in this release had gone into smaller features and feature tweaks. One [...]
[...] Groves, no parece casual ya que anteriormente, podemos encontrar en su blog, y concretamente en Casting an Interface Reference to the Implementing Class in Delphi 2010 de fecha 16 deAgosto de 2009, una referencia a una de las novedades introducidas en Delphi 2010, [...]
[...] http://www.malcolmgroves.com/blog/?p=500 says that this is (newly) implemented in Delphi 2010, which strongly suggest that it wasn’t possible before. Is this indeed the case, or is there a way I’m not familiar with? RTTI, maybe? [...]
[...] en su blog varias entradas abordando aspectos relacionados con las interfaces. Concretamente en Casting an Interface Reference to the Implementing Class in Delphi 2010 de fecha 16 deAgosto de 2009, hace una referencia a una de las novedades introducidas en Delphi [...]