virtual-by-default rant

Artur Skawina art.08.09 at gmail.com
Sat Mar 24 07:43:07 PDT 2012


On 03/23/12 20:11, Adam D. Ruppe wrote:
> 
> http://dlang.org/traits.html#getVirtualFunctions
> 
> if the name isn't on a list of approved virtuals,
> static assert fail.

And you could probably do it in a clean and unintrusive way,
by, for example, extending Object, or doing it on a per-module
basis. If it actually worked...

The obvious problem is that the 'virtual' check pretty much
has to *prevent* devirtualization, or lie about it and always
report the functions as virtual -- otherwise devirtualization
has to happen before the checks, and this could change their
results.

Also, my gdc (which i haven't updated for a while) does not
even devirtualize this simple case:

-------------------------------------------------------
private class C {
  int bar() { return 42; }
}

pragma(attribute, externally_visible) int main() {
  C c = new C;
  //writeln(__traits(isVirtualFunction, c.bar));
  return c.bar();
}
-------------------------------------------------------

It needs at least the class or method marked as 'final' to do the
right thing. Note that the isVirtualFunction check returns true even
for the cases where bar() does get inlined. (*VirtualMethod* do not
exist here)

artur


More information about the Digitalmars-d mailing list