digitalmars.D.bugs - Undefined behaviour using interfaces
- MicroWizard (86/86) Feb 07 2006 What should 'writefln(var)' call when 'var' is interface?
What should 'writefln(var)' call when 'var' is interface? What kind of 'toString' is called for interfaces? If 'too few' methods are defined, then result is Access Violation, which is sane. If 'enough' methods are defined, then D calls 'some' method or picks up 'some' data. Is it normal or is it a bug? Tamás Nagy import std.stdio; import std.string; interface StringAlike { char[] Display(); } class MyString:StringAlike { char[] disp; public: this(char[] st) { disp=st.dup; } char[] Display() { return disp; } } void main(char[][] arg) { MyString a=new MyString("Ali Baba"); writefln("a:",a); writefln("a.Display:",a.Display); StringAlike sa=a; writefln("sa:",sa); writefln("sa.Display:",sa.Display); } a:MyString a.Display:Ali Baba sa:Error: Access Violation import std.stdio; import std.string; interface StringAlike { char[] Display(); char[] Compare(); } class MyString:StringAlike { char[] disp; char[] comp; public: this(char[] st) { disp=st.dup; comp=toupper(st); } char[] Display() { return disp; } char[] Compare() { return comp; } } void main(char[][] arg) { MyString a=new MyString("Ali Baba"); writefln("a:",a); writefln("a.Display:",a.Display); writefln("a.Compare:",a.Compare); StringAlike sa=a; writefln("sa:",sa); writefln("sa.Display:",sa.Display); writefln("sa.Compare:",sa.Compare); } a:MyString a.Display:Ali Baba a.Compare:ALI BABA sa:ALI BABA sa.Display:Ali Baba sa.Compare:ALI BABA
Feb 07 2006