template this parameters for constructors

Gor Gyolchanyan gor.f.gyolchanyan at gmail.com
Wed Feb 20 07:00:21 PST 2013


On Wed, Feb 20, 2013 at 6:42 PM, Jacob Carlborg <doob at me.com> wrote:

> On 2013-02-20 15:37, Gor Gyolchanyan wrote:
>
>  After I tested this, immediately thought of the constructor. The
>> constructor of the base class is guaranteed to be called when
>> constructing a derived class, right? Even if the call is implicit, it
>> still happens and at the call site the concrete type being constructed
>> is known, so in theory, a template this parameter on the base class's
>> constructor should allow the base class to know the static type of the
>> object being constructed (which is immensely useful for implementing
>> dynamic dispatch classes).
>>
>> class Base
>> {
>>      this(this This_)()
>>      {
>>          // Build a dispatch table using __traits(allMethods, This_)
>>      }
>>
>>      void opCall(Payload_)(Payload_ payload_)
>>      {
>>          // Dynamically dispatch payload_ using the previously built
>> dispatch table.
>>      }
>> }
>>
>> class Derived: Base
>> {
>>      // implicitly generated constructor will call the Base.this() and
>> implicitly give it the static type of Derived.
>> }
>>
>> unittest
>> {
>>      Base b = new Derived;
>>      b(); // 100% transparent dynamic dispatch
>> }
>>
>> Unfortunately, this is what DMD had to say about this:
>>
>> C:\Users\g.gyolchanyan\**Desktop\test.d(3): Error: found 'This_' when
>> expecting ')'
>> C:\Users\g.gyolchanyan\**Desktop\test.d(3): Error: semicolon expected
>> following function declaration
>> C:\Users\g.gyolchanyan\**Desktop\test.d(3): Error: Declaration expected,
>> not ')'
>>
>> It looks like DMD thinks I was gonna write a postblit constructor and I
>> guess the functionality is already there, but can't be reached due to a
>> parsing error.
>>
>
> I haven't even thought about this use case. It would be so cool.
>
> --
> /Jacob Carlborg
>

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).
No statically-typed and no dynamically-typed language can combine
flexibility and performance right now.
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.

By the way, what's up with the new reflection thing that Andrei once wrote
about?

-- 
Bye,
Gor Gyolchanyan.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20130220/4602159b/attachment.html>


More information about the Digitalmars-d mailing list