July 03, 2012
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?
July 03, 2012
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
July 03, 2012
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?
July 03, 2012
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
July 03, 2012
Ok, everything works great now.

thank you
maarten
Top | Discussion index | About this forum | D home