passing duck-typed objects and retaining full type information
Jesse Phillips via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Nov 12 19:34:10 PST 2014
On Tuesday, 11 November 2014 at 19:23:39 UTC, Adam Taylor wrote:
> Now what i want to do is pass some such "container" object to
> an interface function:
>
> interface MyInterface
> {
> void filterCollisions(S)(S collisionCandidates)
> if(canRemoveFromContainer!S)
> }
You can't. This is one of the major restrictions of the template
approach. Generics in C# allow for this, which I believe is
reliant its JIT compiler.
Once you introduce the need for a vTable (which interfaces do) it
is no longer possible to require implementation of a "phantom"
function. Templates don't exist until their use, so it is not
possible to require an entry in the vTable when the number of
possible combinations is infinite.
More information about the Digitalmars-d-learn
mailing list