www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - bad unary function

reply maarten van damme <maartenvd1994 gmail.com> writes:
I  have an array of structs and I've written a function that takes as
argument a struct (and an array but this is besides the point) and
returns a unique string.

I wanted to use map to retrieve a string array of all unique strings
from that array. I've used it this way:
string [] added=map!("toName(a,parsedschema)")(mod.added);

But I get a compilation error:

bad unary function : toName(a,parsedschema) for type randomStruct.

Is my usage of map wrong?
what's the compiler trying to explain?
Jul 03 2012
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
maarten van damme:

 string [] added=map!("toName(a,parsedschema)")(mod.added);

Try with a lambda: string[] added = mod.added.map!(x => toName(x, parsedschema))(); Bye, bearophile
Jul 03 2012
prev sibling next sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
Ok, that made sense :)

Now I get a variable with the Result type and this isn't castable or
convertable to an array of strings, how can I extract the actual
result now?
Jul 03 2012
prev sibling next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
maarten van damme:

 Now I get a variable with the Result type and this isn't 
 castable or convertable to an array of strings,

Right, sorry. (If I don't run the code I show, then it's usually broken in some way).
 how can I extract the actual result now?

Often you don't need an array, a lazy range is enough for many purposes. But if you really need an array of strings, then write something like: const added = mod.added.map!(x => x.toName(parsedschema))().array(); array() is inside the module std.array. Bye, bearophile
Jul 03 2012
prev sibling parent maarten van damme <maartenvd1994 gmail.com> writes:
Ok, everything works great now.

thank you
maarten
Jul 03 2012