New set class

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Mon Feb 26 04:29:53 PST 2007


Michiel wrote:
> Frits van Bommel wrote:
> 
>>>> But if you really want to do put such things in your code you could try
>>>> this:
>>>> struct Set(T) {
>>>>     ...
>>>>     void opApplyReverse()() {static assert(0,"don't be silly");}
>>>> }
>>>>
[snip Michiel's disbelief and my explanation]
>> Here, let me show you (some modifications made to above code)
>> <SNIP>
> 
> Interesting trick. I missed the extra (). One problem, though. If I use
> it like this:
> 
> foreach_reverse(e ; x) { }
> 
> Not specifying the element type (int). I get the "cannot infer type"
> error, instead of our custom error message. Why can't the compiler infer
> the type? Isn't it right in its face?

The code you <SNIP>ped didn't have that problem, so I presume you used 
the declaration Bill Baxter posted. Guess why I changed it for my 
example ;).
The compiler can't infer the type in Bill's code because his 
opApplyReverse has the wrong signature. In order to get type deduction 
(as well as instantiation) opApplyReverse needs to have as parameter a 
delegate with the correct number of (inout) arguments that returns int.
In my code I had
---
void opApplyReverse()(int delegate(inout T)) { static assert(0,"don't be 
silly");}
---
and it worked.



More information about the Digitalmars-d mailing list