why can't structs implement interfaces?
wakko
dm at liquidstate.eu
Wed Nov 25 12:29:37 PST 2009
Saaa a écrit :
> struct S : Pos {}
> Why is this not possible?
>
It's not and structures have no vtable... fortunately.
But, could it be a good idea to use the "inheritance operator" to do
some kind of static inheritance like we do with mixins or to force
implementation at compile-time ?
1. inheriting from an interfaces would force function implementation at
compile time:
interface IFoo
{
void foo();
}
// Would not compile
// "Struct S static interface function IFoo.foo isn't implemented"
struct S : IFoo {}
// Would compile
struct S : IFoo { void foo() {} }
2. inheriting from a structure would do some kind a mixin.
struct S1 { void foo(); static void bar(); }
struct S2 : S1 {}
S1.bar();
S1 a; a.foo(); a.bar();
3. class and structs could share interfaces for compile time function
name resolution. I think this already work using untyped template arguments.
interface IFoo
{
void foo();
}
class C : IFoo // dynamic inheritance
{
void foo()
{
}
}
struct S : IFoo // static inheritance
{
void foo()
{
}
}
class Bar(IFoo FOO)
{
void bar(FOO thing)
{
thing.foo();
}
}
4. casting could be forbidden OR :
struct A { int a; }
struct B : A {} // B implements "int a" as in 2
A a; B b = a; // would do something like b.a = a.a
B b; A a = b; // would do something like a.a = b.a
--
AF
More information about the Digitalmars-d-learn
mailing list