Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

byexample/code-generation-parser example doesn't compile #261

Closed
SMietzner opened this issue May 17, 2018 · 5 comments
Closed

byexample/code-generation-parser example doesn't compile #261

SMietzner opened this issue May 17, 2018 · 5 comments

Comments

@SMietzner
Copy link
Contributor

`rdmd playground.d

onlineapp.d(14): Error: template onlineapp.main.each!((e) => conf.parse(e)).each cannot deduce function from argument types !()(Result), candidates are:
/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(904): onlineapp.main.each!((e) => conf.parse(e)).each(Range)(Range r) if (!isForeachIterable!Range && (isRangeIterable!Range || __traits(compiles, typeof(r.front).length)))
/dlang/dmd/linux/bin64/../../src/phobos/std/algorithm/iteration.d(943): onlineapp.main.each!((e) => conf.parse(e)).each(Iterable)(auto ref Iterable r) if (isForeachIterable!Iterable || __traits(compiles, Parameters!(Parameters!(r.opApply))))
`

https://tour.dlang.org/tour/en/byexample/code-generation-parser
https://github.com/dlang-tour/english/blob/master/byexample/code-generation-parser.md

@wilzbach
Copy link
Member

Thanks a lot for the ping.
I added a quick fix from my phone: #262

I don't know how this passed and got through our CI systems, but I will investigate once I am back home in a few days.

@SMietzner
Copy link
Contributor Author

SMietzner commented May 19, 2018

You've added extra curly brackets in the switch statement, and it does compiles now.
The reason for this should be explained in description, because it is not obvious why these are mandatory.

@SMietzner
Copy link
Contributor Author

I would propose this as example code:

import std.algorithm, std.conv, std.range,
        std.stdio, std.traits;
struct Config
{
    int runs, port;
    string name;
}
void main()
{
    Config conf;
    // use the generated parser for each entry
    "runs=1,port=2,name=hello"
        .splitter(",")
        .each!(e => conf.parse(e));
    conf.writeln;
}
void parse(Conf)(ref Conf c, string entry)
{
    auto r = entry.splitter("=");
    auto key = r.front, value = r.dropOne.front;
    Switch: switch (key)
    {
        static foreach (idx, field; Conf.tupleof)
        { 
            case field.stringof:
                c.tupleof[idx] =value.to!(typeof(field));
                break Switch;
        }
        default:
        assert (0, "Unknown member name.");
    }
}

@wilzbach
Copy link
Member

Good idea. I just wanted to fix it.
Would you mind opening a PR for your proposed improvement?
(it's really hard to do this from my phone and I am still backpacking for a few days)

@ghost
Copy link

ghost commented Jun 3, 2019

works for me

@ghost ghost closed this as completed Jun 3, 2019
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants