## D - D and covariant assignment

• Friedrich Dominicus (30/30) Aug 12 2003 As I understand this will work
• Mike Wynn (23/51) Aug 12 2003 yes, D is statically typed (like Java, C++ Delphi etc) unlike Lua, self
• Ilya Minkov (15/20) Aug 12 2003 These things must be obvious to you! BTW, how's your own "Q" language do...
• Mark Evans (9/10) Aug 12 2003 That's one more for the obscure languages list. The only Q that I had k...
• Friedrich Dominicus (8/22) Aug 14 2003 Well I had a wrong example on my mind. I rewrote the stuff and found the
Friedrich Dominicus <frido q-software-solutions.com> writes:
As I understand this will work
class A
{
public int foo () {return 0;}
}

class B: A
public int foo(){ return 1;}
}

A a = new B();
a.foo -> yields 1

But that won't work
class A {
public int foo () { return 0; }
}

class B: A{
public int foo () { return 1; }
public int bar() { return 0;}
}

int main (){
A a = new B();
printf("a.foo() = %d\n", a.foo());
printf("a.bar() = %d\n", a.bar());
return 0;
}

So D allows sort of covariant assignment, but for accessing a functoin
defined in a descendant the function to be accessed must be part
of the ancestor too. In the second example bar must be defined in A too.

Is that correct?

Regards
Friedrich
Aug 12 2003
"Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Friedrich Dominicus" <frido q-software-solutions.com> wrote in message
news:bhakdt\$o1s\$1 digitaldaemon.com...
As I understand this will work
class A
{
public int foo () {return 0;}
}

class B: A
public int foo(){ return 1;}
}

A a = new B();
a.foo -> yields 1

But that won't work
class A {
public int foo () { return 0; }
}

class B: A{
public int foo () { return 1; }
public int bar() { return 0;}
}

int main (){
A a = new B();
printf("a.foo() = %d\n", a.foo());
printf("a.bar() = %d\n", a.bar());
return 0;
}

So D allows sort of covariant assignment, but for accessing a functoin
defined in a descendant the function to be accessed must be part
of the ancestor too. In the second example bar must be defined in A too.
yes, D is statically typed (like Java, C++ Delphi etc) unlike Lua, self ..... so A a; although a can hold a reference to B or any other sub class of A you have to cast a to the subclass before using those "messages" or "methods" i.e.
printf("a.bar() = %d\n", (cast(B)a).bar());
cast(B)a will return null if a is not castable to B class C { .. } you could not do cast(C)a as there is not way that `a` can be a subclass of C (single inheritance only) the types and inheritance tree for types are all determinded at compile time late binding is performed via a virtual function table (vtbl) so a.foo() will be converted to evoke a->vtbl[index of method foo]( a, params to foo ); the compiler has to be able to determine the vtbl index at compile time (not runtime) you may have class D : A { void ping() { } int bar() { return 44; } } a could be a B or D; the vtable entry for bar might be 2 (B) 3 (D) etc ...
Aug 12 2003
Ilya Minkov <midiclub 8ung.at> writes:
Friedrich Dominicus wrote:

So D allows sort of covariant assignment, but for accessing a functoin
defined in a descendant the function to be accessed must be part
of the ancestor too. In the second example bar must be defined in A too.

Is that correct?
These things must be obvious to you! BTW, how's your own "Q" language doing? In your example "a" is of type "A", which doesn't have a "bar()" defined. If you think you know that a is actually of type B, you can go ahead and do so: int main (){ A a = new B(); printf("a.foo() = %d\n", a.foo()); printf("a.bar() = %d\n", cast(B) (a.bar()) ); return 0; } cast will reward you with an exception, if you were wrong. Can you do such acrobacy in Eiffel or Q as you have tried to suggest with your example? -i.
Aug 12 2003
Ilya Minkov says...
BTW, how's your own "Q" language doing?
That's one more for the obscure languages list. The only Q that I had known about lived here. http://www.musikwissenschaft.uni-mainz.de/~ag/q/q.php Friedrich Dominicus' Q language lives here http://www.q-software-solutions.com/about.shtml (Almost all single-letter language names are taken, for those contemplating a new one.) Mark
Aug 12 2003
Friedrich Dominicus <frido q-software-solutions.com> writes:
Ilya Minkov wrote:
Friedrich Dominicus wrote:

So D allows sort of covariant assignment, but for accessing a functoin
defined in a descendant the function to be accessed must be part
of the ancestor too. In the second example bar must be defined in A too.

Is that correct?
These things must be obvious to you! BTW, how's your own "Q" language doing?
Well I had a wrong example on my mind. I rewrote the stuff and found the that D is correct and I was wrong.

Can you do such acrobacy in Eiffel or Q as you have tried to suggest