delegate, template and alias

Timon Gehr timon.gehr at gmx.ch
Mon Dec 19 15:23:11 PST 2011


On 12/19/2011 09:30 PM, Philippe Sigaud wrote:
> On Mon, Dec 19, 2011 at 18:49, Timon Gehr<timon.gehr at gmx.ch>  wrote:
>> On 12/19/2011 06:46 PM, Philippe Sigaud wrote:
>>>
>>> On Mon, Dec 19, 2011 at 15:35, Heromyth<bitworld at qq.com>    wrote:
>>>>
>>>> Woo, I got it.
>>>
>>>
>>> What's the difference with your first post?
>>
>>
>> He uses an eponymous template now.
>
> Ah yes, thanks. Strange, I'm pretty sure I used the eponymous trick
> and it didn't work, for some reason.
>
> Anyway, Heromyth, I'm slowly working on a D template tutorial. The
> current pdf is at:
>
> https://github.com/PhilippeSigaud/D-templates-tutorial/blob/master/dtemplates.pdf
>
> (click on View Raw to download).
>
> It's still incomplete. I'll work on it during the holidays and post on
> D.announce to get some feedback on it, but you're welcome if you want
> to give it a try.
>
>
> Philippe

Looks very good.

Knowing it is a work in progress, I will already give some feedback:

You have a typo on page 15:
static if (is(T t = U[], U)) // is T an array of U, for some type U?

You probably meant to write

static if (is(T t == U[], U)) // is T an array of U, for some type U?

On page 18, I think your explanation why you used .power instead of 
power is not yet entirely accurate. The only reason why it is needed is 
because you are defining an eponymous template that hides the global 
template.

Also on page 18:

'Note that this template will work not only for unary (one argument) 
functions but also for n-args functions,'

The implementation given does not work for n-args functions because 
functions cannot return a tuple.

Page 23:

'Note that flatten works perfectly on ranges too, but is not lazy'

Ranges don't have the concatenation operator, so it will not work.

Page 25:

'And due to (to my eyes) a bug
in alias, you cannot alias them to a symbol:

alias (a){ return a;} Id; // Error'

It is a restriction that the current D grammar has. You can do this:

alias ID!((a){ return a;}) Id;

where ID is

template ID(alias X){alias X ID;}

Page 25:

'Since they are delegates, they can capture local symbols, as long as 
these are defined at compile-time:'

They can even capture local symbols that don't have a compile-time 
value. For example:

auto foo(alias s)(){return s();}

void main() {
     int	x = 2;
     writeln(foo!({return x;})()); // prints '2'
}

And the link is purely symbolic, no closures or the like are allocated.

What the compiler effectively does is in the lines of this:

void main() {
     int x = 2;
     auto dgliteral() { return x; } // {return x;}
     auto foo(){ return dgliteral(); } // foo!({return x})
     writeln(foo()); // call template instance
}

This is one of the most powerful features of D templates: They are 
instantiated in the most local scope that is required for them to work.

Page 38:

'By the way, strangely enough, though you cannot declare ‘pure’ 
templates inside functions, you can declare struct templates.'

Do you want to file a bug report against this? This has bothered me a 
few times too.

Page 39:

Template this parameters are also useful to determine how the type of 
the this reference is qualified, (const/immutable/shared/inout)

Page 67:

'mixin template Concatenate()
{
     Tuple!(This, U) opBinary(string op, this This)(This u)
     if (op == "~")
     {
         return tuple(this, u);
     }
     Tuple!(U, This) opBinaryRight(string op, this This)(This u)
     if (op == "~")
     {
         return tuple(u, this);
     }
}'

I think what you meant was this:

mixin template Concatenate()
{
     Tuple!(This, U) opBinary(string op, this This)(U u)
     if (op == "~")
     {
         return tuple(this, u);
     }
     Tuple!(U, This) opBinaryRight(string op, this This)(U u)
     if (op == "~")
     {
         return tuple(u, this);
     }
}

Page 76:

'At the time of this writing,
the limitations are mostly: no classes and no exceptions (and so, no 
enforce).'

DMD 2.057 supports classes and exceptions in CTFE =)


Page 82:

I'd be careful with that table. Probably most of the ones you marked as 
'No' are actually 'Yes' when doing some clever things with is(typeof())?

Page 90:

'34 Extending a Class'

Maybe add a note that this does not work across module boundaries and 
therefore has not much practical use.

Page 90:

'return;
// or
return void;'

That looks like it was actual syntax.

Page 94/95:
'Strangely, you can only use it with the is(Type identifier, ...) 
syntax: you must have identifier.'

'For me, the main limitation is that template tuple parameters are not 
accepted. Too bad.'

Do you want to file the enhancement requests, or should I file them? 
Having those would increase the consistency of the language.



Keep up the good work!



More information about the Digitalmars-d-learn mailing list