On Wednesday, 1 April 2015 at 18:13:15 UTC, Ali Çehreli wrote:
On 04/01/2015 10:57 AM, Dzugaru wrote:

> ElementType!S aggregate(alias func, S)(S list, ElementType!S
accum =
> ElementType!S.init)
> if(is(typeof(func(accum, accum)) == typeof(accum))) {
[...]
> }

I can't explain exactly why that doesn't work.

However, I've discovered a number of times that reducing the dependency between template parameters helps. Instead of using ElementType!S in the parameter list, introduce a third one (E), which you check in the template constraint:

ElementType!S aggregate(alias func, S, E)(S list, E accum = E.init)
if(is (E == ElementType!S) &&                   // <-- ADDED
    is(typeof(func(accum, accum)) == typeof(accum))) {
        // ...
}

Now it works.

Ali

Great tip. Is that in your book somewhere?

Reply via email to