Idea: Lazy upcasting
BCS
ao at pathlink.com
Tue Mar 27 15:20:44 PDT 2007
Reply to Reiner,
> PS. The reason I needed to add the extra interface was that D's type
> system doesn't think StorageImpl!(Derived) is-a StorageImpl!(Base)
>
> It makes sense, because there are times when such a conversion is
> dangerous:
> List!(Square) a;
> List!(Shape) b = a;
> b.add(new Triangle); /// Ooops
> But Java and Nemerle support this kind of casting in a safe way (I
> think). What you need to do is specify something in the template
> parameters at the top, which says, "You can cast down from this" or
> "you can cast up from this". The general rule is that the "you can
> cast down from this" types may only be used as return values, and the
> "you can cast up from this" types may only be used as parameters:
>
> interface Reader(T) // you can cast down from this
> {
> T read();
> }
> // Reader!(Derived) is-a Reader!(Base)
>
> interface Writer(T) // you can cast up from this
> {
> void write(T);
> }
> // Writer!(Base) is-a Writer!(Derived)
>
I haven't been following what you are looking for but this might get some
of what you want.
template Foo(T)
{
static if(is(T.superof))
interface Foo : Foo!(T.superof)
{
...
}
else
interface Foo
{
...
}
}
More information about the Digitalmars-d
mailing list