<div dir="ltr">On 4 June 2013 18:23, Jacob Carlborg <span dir="ltr"><<a href="mailto:doob@me.com" target="_blank">doob@me.com</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
In the thread "Slow performance compared to C++, ideas?" Manu is arguing that methods should be non-virtual by default. The problem he is having is that developers will forget to declare methods as final.<br>
<br>
This issue got me think if there's a way to automatically detect that a method is virtual that shouldn't be.<br>
<br>
If one modifies druntime to take advantage out of RTInfo. If I understand everything correctly RTInfo will be instantiated once for each user defined type. With that and the help of a UDA you can check that all methods that should be final really are final. Something like:<br>

<br>
struct virtual {} // the UDA<br>
<br>
class Foo<br>
{<br>
    void a () {} // static assert, not declared as @virtual<br>
    @virtual void b () {} // ok<br>
    @virtual final void c () {} // static assert, declared as final and @virtual<br>
    final d () {} // ok<br>
}<br>
<br>
checkVirtual!(Foo);<br>
<br>
import std.typetuple;<br>
<br>
void checkVirtual (T) ()<br>
{<br>
    static if (!is(T == class))<br>
        return;<br>
<br>
    foreach (m ; __traits(derivedMembers, T))<br>
    {<br>
        alias TypeTuple!(__traits(<u></u>getAttributes, mixin("T." ~ m))) attrs;<br>
        enum methodName = T.stringof ~ "." ~ m.stringof;<br>
<br>
        static if (staticIndexOf!(virtual, attrs) != -1)<br>
        {<br>
            static if (!__traits(isVirtualMethod, mixin("T." ~ m)))<br>
                static assert (false, "Method " ~ methodName ~ " marked as @virtual is not virtual");<br>
        }<br>
<br>
        else<br>
        {<br>
            static if (__traits(isVirtualMethod, mixin("T." ~ m)))<br>
                static assert (false, "Method " ~ methodName ~ " is virtual but not marked as @virtual");<br>
        }<br>
    }<br>
}<span class="HOEnZb"><font color="#888888"><br>
<br>
-- <br>
/Jacob Carlborg<br>
</font></span></blockquote></div><br></div><div class="gmail_extra" style>This is a great idea. If final-by-default is ultimately rejected, I'll definitely try this out.</div><div class="gmail_extra" style>That said, it only solves half the problem, that is, internal programmers forgetting to mark their functions final (and assuming they also remember 'checkVirtual!' in their moules).</div>
<div class="gmail_extra" style>It offers nothing to address the 3rd party library problem.</div></div>