Re: toString() through interface

2014-04-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On Sat, 19 Apr 2014 20:51:49 -0400, David Held wrote: On 4/19/2014 5:35 PM, David Held wrote: interface Foo { } class Bar : Foo { override string toString() pure const { return "Bar"; } } void main() { Foo foo = new Bar; foo.toString(); } To make things more interesting, con

Re: toString() through interface

2014-04-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On Sat, 19 Apr 2014 20:45:50 -0400, Adam D. Ruppe wrote: On Sunday, 20 April 2014 at 00:35:30 UTC, David Held wrote: Since all implementations of an interface must derive from Object That's not true. They can also come from IUnknown or a C++ interface. This is also not true. Only interfa

Re: toString() through interface

2014-04-19 Thread bearophile via Digitalmars-d-learn
David Held: class Baz { override string toString() pure const { return cast(Object)(foo).toString(); } Foo foo; } This really makes the compiler go bonkers: src\Bug.d(11): Error: pure nested function 'toString' cannot access mutable data 'foo' src\Bug.d(11): Error: pure nested fu

Re: toString() through interface

2014-04-19 Thread David Held via Digitalmars-d-learn
On 4/19/2014 5:45 PM, Adam D. Ruppe wrote: On Sunday, 20 April 2014 at 00:35:30 UTC, David Held wrote: Since all implementations of an interface must derive from Object That's not true. They can also come from IUnknown or a C++ interface. Ok, that's a good reason! cast(Object)(foo).toS

Re: toString() through interface

2014-04-19 Thread David Held via Digitalmars-d-learn
On 4/19/2014 5:35 PM, David Held wrote: interface Foo { } class Bar : Foo { override string toString() pure const { return "Bar"; } } void main() { Foo foo = new Bar; foo.toString(); } To make things more interesting, consider the call to toString() from inside a class (which

Re: toString() through interface

2014-04-19 Thread Adam D. Ruppe via Digitalmars-d-learn
On Sunday, 20 April 2014 at 00:35:30 UTC, David Held wrote: Since all implementations of an interface must derive from Object That's not true. They can also come from IUnknown or a C++ interface. cast(Object)(foo).toString(); (cast(Object)foo).toString() might work This might return

toString() through interface

2014-04-19 Thread David Held via Digitalmars-d-learn
interface Foo { } class Bar : Foo { override string toString() pure const { return "Bar"; } } void main() { Foo foo = new Bar; foo.toString(); } src\Bug.d(14): Error: no property 'toString' for type 'Bug.Foo' Since all implementations of an interface must derive from Object, why c