www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Broken NVI support

reply "Maxim Fomin" <maxim maxim-fomin.ru> writes:
Hello.
Please, put attention to following non-runnable due to linker 
errors sample http://dpaste.dzfl.pl/cad4b558.

I suspect that that NVI support is broken, because (my guess) 
linker assumes that interface should implement functions and 
doesn't look to class definition.

After reading relevant section in TDPL I found another issue: 
according to p.215 bar() and baz() may have (or even should) 
'override' specifier, but currently dmd rejects it. Moreover, dmd 
is irrelevant about whether 'bar' and 'baz' functions (within 
class definition) are specified with private keyword or not: is 
is a bug or intentional behavior?

If interface implements functions, which are called from another 
of interface's functions, linking goes fine. However, after 
rereading relevant TDPL paragraph and this Alexandrescu post [1] 
I think that the trick is that functions 'bar' and 'baz' are 
implemented *not* in interface but in class-implementer.

Furthermore, if function declarations are converted to empty 
definitions keeping the same class definitions, dmd accepts this 
ambiguity (class A.bar() and interface I.bar()) and gives 
priority to interface's functions on calling them.

So, is this a bug or my NVI misunderstanding is wrong and what is 
D design policy in this issue?

[1] 
http://digitalmars.com/d/archives/digitalmars/D/The_Non-Virtual_Interface_idiom_in_D_96739.html
Aug 13 2012
next sibling parent "Era Scarecrow" <rtcvb32 yahoo.com> writes:
On Monday, 13 August 2012 at 18:48:09 UTC, Maxim Fomin wrote:
 Hello.
 Please, put attention to following non-runnable due to linker 
 errors sample http://dpaste.dzfl.pl/cad4b558.

[quote] interface I { private: void bar(); void baz(); final void foo() [/quote] Isn't it in the specs that overriding methods need to be public functions? And interfaces are effectively for public interface only so there shouldn't be any private methods? I mean, what's the point in mentioning bar and baz if you can't call them? Or even foo in this case....
Aug 13 2012
prev sibling next sibling parent "Simen Kjaeraas" <simen.kjaras gmail.com> writes:
On Mon, 13 Aug 2012 20:48:07 +0200, Maxim Fomin <maxim maxim-fomin.ru>  
wrote:

 Hello.
 Please, put attention to following non-runnable due to linker errors  
 sample http://dpaste.dzfl.pl/cad4b558.

As Era pointed out, private functions in D are implicitly final, i.e. not overridable. Change all instances of 'private' to 'protected', and Bob is your proverbial uncle. -- Simen
Aug 13 2012
prev sibling next sibling parent "Jonathan M Davis" <jmdavisProg gmx.com> writes:
On Monday, August 13, 2012 20:48:07 Maxim Fomin wrote:
 So, is this a bug or my NVI misunderstanding is wrong and what is
 D design policy in this issue?

Currently, only public and protected functions are ever virtual: http://d.puremagic.com/issues/show_bug.cgi?id=4542 This may or may not change for interfaces in order to match TDPL, but I wouldn't expect it to ever change for classes. You should be able to do NVI just fine with protected. - Jonathan M Davis
Aug 13 2012
prev sibling parent "Maxim Fomin" <maxim maxim-fomin.ru> writes:
Thanks for replies, will use protected.
Aug 13 2012