confused about string and lambda args
Adam D. Ruppe
destructionator at gmail.com
Thu Jan 16 17:11:11 UTC 2020
On Thursday, 16 January 2020 at 17:03:33 UTC, mark wrote:
> auto wordCharCounts = words // I added this and it works
> fine
> .map!"a.length";
> writeln(wordCharCounts);
The string thing probably shouldn't be used anymore. I suggest
you always use the => form instead.
The string thing is a legacy version that was before the language
had =>.
> I don't understand why both syntaxes work for .length but only
> the string form for .count?
It is because of imports.
So the string version passes the string to the library, which
pastes it into some skeleton code and makes a function out of it.
It basically does:
string code = "import some_stuff; (a) { return " ~
your_string ~ "; }";
mixin(code);
Note it does this INSIDE the library.
It is that `import some_stuff;` that accounts for this
difference. The string one pastes in some library imports so some
functions are available. The => form does not.
Since the string one is inside the lib, it can NOT see your own
functions from your module! But since the lib imports a few other
library modules, it may be able to see things your module didn't
import.
The better way to do it is to use your => format, but go ahead
and import the necessary module.
I believe `count` is located in `import std.algorithm;`. So add
that to your module and it should work now.
More information about the Digitalmars-d-learn
mailing list