Clay language
Steven Schveighoffer
schveiguy at yahoo.com
Thu Dec 30 07:50:05 PST 2010
On Thu, 30 Dec 2010 10:22:22 -0500, Michel Fortin
<michel.fortin at michelf.com> wrote:
> On 2010-12-30 10:00:05 -0500, "Steven Schveighoffer"
> <schveiguy at yahoo.com> said:
>
>> The thing I find ironic is that with the original operator overloading
>> scheme, the issue was that for types that define multiple operator
>> overloads in a similar fashion, forcing you to repeat boilerplate code.
>> The solution to it was a mixin similar to what you are suggesting.
>> Except now, even mundane and common operator overloads require verbose
>> template definitions (possibly with mixins), and it's the uncommon
>> case that benefits. So really, we haven't made any progress (mixins
>> are still required, except now they will be more common). I think
>> this is one area where D has gotten decidedly worse. I mean, just
>> look at the difference above between defining the opcat operator in D1
>> and your mixin solution!
>
> I'm with you, I preferred the old design.
>
>
>> As a compromise, can we work on a way to forward covariance, or to have
>> the compiler reevaluate the template in more derived types?
>
> I stubbled upon this yesterday:
>
> Template This Parameters
>
> TemplateThisParameters are used in member function templates to pick up
> the type of the this reference.
> import std.stdio;
>
> struct S
> {
> const void foo(this T)(int i)
> {
> writeln(typeid(T));
> }
> }
>
> <http://www.digitalmars.com/d/2.0/template.html>
>
> Looks like you could return the type of this this way...
>
Damn! That's very very close to what I wanted! Thanks for stumbling on
that ;) I don't even need to repeat it at derived levels (just tried it
out).
The one issue I see now is, I have to cast this to T, which involves a
runtime cast. From what I understand, covariance does not require a
runtime cast penalty, because the adjustment lookup is done at compile
time.
I wonder if the compiler can optimize out the runtime dynamic cast in this
case, Walter?
Now, for a working (but not quite as effecient as D1) solution, all I need
is the bug fix for allowing templates in interfaces.
-Steve
More information about the Digitalmars-d
mailing list