Thread overview
confused about string and lambda args
Jan 16, 2020
mark
Jan 16, 2020
Adam D. Ruppe
Jan 17, 2020
mark
Jan 17, 2020
mark
Jan 16, 2020
H. S. Teoh
January 16, 2020
I'm looking at https://tour.dlang.org/tour/en/gems/range-algorithms
(IMO the example code is far too long and complicated.)
But here's the thing:

    auto wordCharCounts = words // I added this and it works fine
        .map!"a.length";
    writeln(wordCharCounts);

    auto wordCharCounts2 = words // I added this and it works fine
        .map!(a => a.length);
    writeln(wordCharCounts2);

    auto wordCharCounts3 = words // this is in the tutorial
      .map!"a.count";
    writeln(wordCharCounts3);

    auto wordCharCounts4 = words // I added this and it won't compile
      .map!(a => a.count); // Error: no property count for type string
    writeln(wordCharCounts4);

I don't understand why both syntaxes work for .length but only the string form for .count?
January 16, 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.
January 16, 2020
On Thu, Jan 16, 2020 at 05:03:33PM +0000, mark via Digitalmars-d-learn wrote: [...]
>     auto wordCharCounts4 = words // I added this and it won't compile
>       .map!(a => a.count); // Error: no property count for type string
>     writeln(wordCharCounts4);

You need to import std.algorithm to get `count`.


> I don't understand why both syntaxes work for .length but only the string form for .count?

Because .length is a property of strings, whereas .count is actually not a string property, but a function that's being called via UFCS: Unified Function Call Syntax, in which when the compiler sees something like:

	obj.func(x, y, z);

but `obj` doesn't have a member named `func`, then it will try to rewrite it into:

	func(obj, x, y, z);

instead.


T

-- 
Knowledge is that area of ignorance that we arrange and classify. -- Ambrose Bierce
January 17, 2020
On Thursday, 16 January 2020 at 17:11:11 UTC, Adam D. Ruppe wrote:
[...]
> 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 =>.
[...]

Thanks for that very clear explanation.

I have attempted to fix that part of the tour:
https://github.com/dlang-tour/english/pull/304
January 17, 2020
On Friday, 17 January 2020 at 07:57:16 UTC, mark wrote:
> On Thursday, 16 January 2020 at 17:11:11 UTC, Adam D. Ruppe wrote:
> [...]
>> 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 =>.
> [...]
>
> Thanks for that very clear explanation.
>
> I have attempted to fix that part of the tour:
> https://github.com/dlang-tour/english/pull/304

I (hopefull) deleted the above and replaced it with:
https://github.com/dlang-tour/english/pull/305