digitalmars.D.learn - How to use map?
- Lemonfiend (28/28) Nov 11 2014 I'm trying to do something simple like creating an array of
- bearophile (15/17) Nov 11 2014 Generally don't use casts, unless you know what you are doing
- Lemonfiend (9/19) Nov 11 2014 Sorry, my example had an unneeded template. Simply this (and also
- Lemonfiend (3/3) Nov 11 2014 Oh, no it doesn't. My bad.
- "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> (13/16) Nov 11 2014 Don't know whether it's documented, but it's a consequence of
- Lemonfiend (2/14) Nov 11 2014 That makes sense, thanks.
I'm trying to do something simple like creating an array of struct S from a float array via map: --- void main() { float[] vals = [1.1, 2.1, 3.1, 4.1]; import std.algorithm: map; auto arr = vals.map!`S(a)`.array; writeln(arr); } struct S(T) { T t; } --- But I get: --- C:\D\dmd2\windows\bin\..\..\src\phobos\std\functional.d-mixin-49(49): Error: undefined identifier S C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(480): Error: template instance std.functional.unaryFun!("S(a)", "a").unaryFun!float error instantiating C:\D\dmd2\windows\bin\..\..\src\phobos\std\algorithm.d(427): instantiated from here: MapResult!(unaryFun, float[]) src\app.d(28): instantiated from here: map!(float[]) --- If I instead do ie. map!`cast(int)a` it works fine. What am I missing?
Nov 11 2014
Lemonfiend:If I instead do ie. map!`cast(int)a` it works fine. What am I missing?Generally don't use casts, unless you know what you are doing (and often you don't). The code you were trying to write: struct Foo(T) { T t; } void main() { import std.stdio, std.algorithm, std.array; float[] vals = [1.1, 2.1, 3.1, 4.1]; auto arr = vals.map!(Foo!float).array; arr.writeln; } Bye, bearophile
Nov 11 2014
The code you were trying to write: struct Foo(T) { T t; } void main() { import std.stdio, std.algorithm, std.array; float[] vals = [1.1, 2.1, 3.1, 4.1]; auto arr = vals.map!(Foo!float).array; arr.writeln; }Sorry, my example had an unneeded template. Simply this (and also your code) --- struct S { float f; } --- still results in an undefined identifier.
Nov 11 2014
Oh, no it doesn't. My bad. It was all about !(Foo) vs !(`Foo(a)`). Is there somewhere I can read more about this?
Nov 11 2014
On Tuesday, 11 November 2014 at 14:09:43 UTC, Lemonfiend wrote:Oh, no it doesn't. My bad. It was all about !(Foo) vs !(`Foo(a)`). Is there somewhere I can read more about this?Don't know whether it's documented, but it's a consequence of using string mixins. unaryFun (which is used internally by map) is implemented this way: auto unaryFun(ElementType)(auto ref ElementType __a) { mixin("alias " ~ parmName ~ " = __a ;"); return mixin(fun); } where `fun` is `Foo(a)`. Of course, `Foo` is not visible in std.functional. When you pass in a symbol, the lookup work because it takes place at the point of instantiation.
Nov 11 2014
On Tuesday, 11 November 2014 at 15:53:37 UTC, Marc Schütz wrote:Don't know whether it's documented, but it's a consequence of using string mixins. unaryFun (which is used internally by map) is implemented this way: auto unaryFun(ElementType)(auto ref ElementType __a) { mixin("alias " ~ parmName ~ " = __a ;"); return mixin(fun); } where `fun` is `Foo(a)`. Of course, `Foo` is not visible in std.functional. When you pass in a symbol, the lookup work because it takes place at the point of instantiation.That makes sense, thanks.
Nov 11 2014