Boy I wish that QC was still up and QualityPortal was publicly indexable as that would have saved me quite a bit of time tracking this down. Luckily I got help from Stefan Glienke (who maintains the awesome Spring4D library based on modern Delphi compiler support) when I mentioned
How good are you with reference to function?
I’ve an odd compiler thing throwing errors when using interfaces but not with classes.
So, for posterity:
Unlike C#, in Delphi interface methods are not compatible with method references or methods of object.
This has many manifestations, which means you can get a variety of compiler errors. I’ve listed the ones I could find below, but presume there are more and if I find more will update this post.
These are the errors you can get:
- E2010 Incompatible types: ‘T’ and ‘Procedure’
- E2035 Not enough actual parameters
- E2250 There is no overloaded version of ‘FirstOrDefault’ that can be called with these arguments
These are the (now defunct, but used to be publicly accessible) QC and QualityPortal (needs sign on) entries (thanks Stefan Glienke and Blaise Thorn for reporting these):
- QC95543 An overloaded routine cannot be assigned to the method reference (June 2011)
- QC110364 Interface methods are not assignable to anonymous method variable (November 2015)
- [RSP-13007] Interface methods are not assignable to anonymous method variable – Embarcadero Technologies (November 2016)
The really frustrating part is that the RSP is marked as “new feature” whereas clearly it isn’t, so it probably never will be fixed.
A workaround for now is to wrap the interface method references with:
- either anonymous methods (when you have just a few classes to cover, but maybe more than a few methods on the interface)
- or instance methods on a class (when there are many classes to cover and preferably few methods on the interface)
Examples are in the code below that also shows this works fine and dandy in C#.
–jeroen