<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On 13 August 2015 at 19:12, Steven Schveighoffer via Digitalmars-d <span dir="ltr"><<a href="mailto:digitalmars-d@puremagic.com" target="_blank">digitalmars-d@puremagic.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 8/12/15 8:22 AM, Iain Buclaw wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
This post got me thinking:<br>
<a href="http://forum.dlang.org/post/mpo71n$22ma$1@digitalmars.com" rel="noreferrer" target="_blank">http://forum.dlang.org/post/mpo71n$22ma$1@digitalmars.com</a><br>
<br>
We know at compile time for a given object whether or not there are any<br>
invariants, lack of any polymorphism, along with disallowing invariants<br>
in interfaces means that for the given:<br>
<br>
   class NoInvariants { }<br>
   NoInvariants obj;<br>
   assert(obj);<br>
<br>
It's only a case of checking each base class for any invariant<br>
functions, and if none are found, then we can make an (almost)<br>
reasonable assumption that calling _d_invariant will result in nothing<br>
but wasted cycles.<br>
<br>
However, these can't be omitted completely at compile-time given that we<br>
can't guarantee if there are any up-cast classes that have an invariant.<br>
<br>
But we should be able to speculatively test at runtime whether or not a<br>
call to _d_invariant may be required by doing a simple pointer test on<br>
the classinfo.<br>
</blockquote>
<br></span>
My thought was that you could just set the default invariant pointer to null. Then when you load the invariant function to call, if it's null, don't call it.<br>
<br></blockquote><div><br></div><div>That is what's done at compile time with structs.<br><br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
You could probably get rid of calls to _d_invariant by just calling the invariant directly, no?<br>
<br>
-Steve<br>
</blockquote></div><br></div><div class="gmail_extra">Not with classes, because you need to walk over all interfaces in the vtable, which more likely than not is unknown at compile-time.<br><br></div><div class="gmail_extra">Regards<br></div><div class="gmail_extra">Iain.<br></div><div class="gmail_extra"><br></div></div>