June 29, 2013 Error: this for <method name> needs to be type S not type MapResult!... | ||||
---|---|---|---|---|
| ||||
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 |
July 03, 2013 Re: Error: this for <method name> needs to be type S not type MapResult!... | ||||
---|---|---|---|---|
| ||||
Posted in reply to Peter Neubauer | 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.
|
Copyright © 1999-2021 by the D Language Foundation