Error: this for <method name> needs to be type S not type MapResult!...

Michael pongad at gmail.com
Wed Jul 3 12:07:16 PDT 2013


On Saturday, 29 June 2013 at 19:44:00 UTC, Peter Neubauer wrote:
> Please explain why this error happens in the following code:
>
> import std.algorithm;
>
> struct S
> {
>   void foo ()
>   {
>     int f1 (int a) { return conv(a); }
>     int delegate (int) f2 = &conv;
>
>     int[] x = [1, 2, 3];
>     x.map!conv;    // ERROR
>     x.map!f1;      // fine
>     x.map!f2;      // also fine
>   }
>
>   int conv (int a)
>   {
>     return a+1;
>   }
> }
>
> --- compile output:
>
> /usr/include/d/std/algorithm.d(404): Error: this for conv needs 
> to be type S not type MapResult!(conv, int[])
> /usr/include/d/std/algorithm.d(438): Error: this for conv needs 
> to be type S not type MapResult!(conv, int[])
> /usr/include/d/std/algorithm.d(450): Error: this for conv needs 
> to be type S not type MapResult!(conv, int[])
> /usr/include/d/std/algorithm.d(390): Error: template instance 
> std.algorithm.MapResult!(conv, int[]) error instantiating
> /home/peter/proggen/goliza.reduced/gtp.d(12):        
> instantiated from here: map!(int[])
> /home/peter/proggen/goliza.reduced/gtp.d(12): Error: template 
> instance std.algorithm.map!(conv).map!(int[]) error 
> instantiating
>
> ---
>
> Thanks,
> -Peter

I am not sure if this behavior is intended. Since "conv" is a 
instance method of S, it has an implicit "this" which must be of 
type S. I suspect that the map template tries to invoke conv in 
the context of MapResult. I hope template masters can give you a 
more complete explanation though.


More information about the Digitalmars-d-learn mailing list