<div dir="ltr">On Wed, Feb 20, 2013 at 6:42 PM, 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:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
<div class=""><div class="h5">On 2013-02-20 15:37, Gor Gyolchanyan wrote:<br>
<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex">
After I tested this, immediately thought of the constructor. The<br>
constructor of the base class is guaranteed to be called when<br>
constructing a derived class, right? Even if the call is implicit, it<br>
still happens and at the call site the concrete type being constructed<br>
is known, so in theory, a template this parameter on the base class's<br>
constructor should allow the base class to know the static type of the<br>
object being constructed (which is immensely useful for implementing<br>
dynamic dispatch classes).<br>
<br>
class Base<br>
{<br>
     this(this This_)()<br>
     {<br>
         // Build a dispatch table using __traits(allMethods, This_)<br>
     }<br>
<br>
     void opCall(Payload_)(Payload_ payload_)<br>
     {<br>
         // Dynamically dispatch payload_ using the previously built<br>
dispatch table.<br>
     }<br>
}<br>
<br>
class Derived: Base<br>
{<br>
     // implicitly generated constructor will call the Base.this() and<br>
implicitly give it the static type of Derived.<br>
}<br>
<br>
unittest<br>
{<br>
     Base b = new Derived;<br>
     b(); // 100% transparent dynamic dispatch<br>
}<br>
<br>
Unfortunately, this is what DMD had to say about this:<br>
<br>
C:\Users\g.gyolchanyan\<u></u>Desktop\test.d(3): Error: found 'This_' when<br>
expecting ')'<br>
C:\Users\g.gyolchanyan\<u></u>Desktop\test.d(3): Error: semicolon expected<br>
following function declaration<br>
C:\Users\g.gyolchanyan\<u></u>Desktop\test.d(3): Error: Declaration expected,<br>
not ')'<br>
<br>
It looks like DMD thinks I was gonna write a postblit constructor and I<br>
guess the functionality is already there, but can't be reached due to a<br>
parsing error.<br>
</blockquote>
<br></div></div>
I haven't even thought about this use case. It would be so cool.<span class=""><font color="#888888"><br>
<br>
-- <br>
/Jacob Carlborg<br>
</font></span></blockquote></div><br>This would mean, that D can flawlessly mix together static and dynamic typing to achieve maximum flexibility (using dynamic typing like this) and maximum performance (using static typing when this isn't required).</div>
<div class="gmail_extra"><div style>No statically-typed and no dynamically-typed language can combine flexibility and performance right now.</div><div style>Combined with the upcoming revamp of D's compile-time and (built on top of it) run-time reflection, this will make D the ultimate modeling tool for building systems of arbitrary dynamicity and maximum performance.</div>
<div style><br></div><div style>By the way, what's up with the new reflection thing that Andrei once wrote about?</div><div style><br></div>-- <br>Bye,<br>Gor Gyolchanyan.
</div></div>