A pattern I'd like to see more of - Parsing template parameter tuples

Jonathan M Davis newsgroup.d at jmdavisprog.com
Tue May 22 15:25:47 UTC 2018


On Tuesday, May 22, 2018 14:53:50 Jacob Carlborg via Digitalmars-d wrote:
> On Tuesday, 22 May 2018 at 11:08:13 UTC, Jonathan M Davis wrote:
> > That's basically what dxml does except that it takes advantage
> > of the fact that each member is a different type (because each
> > is a differnt instance of std.typecons.Flag) so that it can
> > have a variadic function which takes any of the arguments in
> > any order. e.g.
> >
> > enum config = makeConfig(SkipComments.yes, SplitOnEmpty.yes);
> > auto range = parseXML!config(xml);
> >
> > or
> >
> > auto range = parseXML!(makeConfig(SkipComments.yes,
> > SplitOnEmpty.yes))(xml);
>
> I don't see how that is any better.
>
> * It requires implementing `makeConfig`
> * It requires using std.typecons.Flag which I think is an ugly
> hack due to the lack of named arguments
> * It only supports boolean types
>
> By using a regular struct it's also possible to specify named
> arguments and in any order:
>
> struct Options
> {
>      bool foo;
>      int value;
>      bool bar;
>      string value2;
> }
>
> Options options = { value2: "asd", bar: true, foo: false };
>
> --

Honestly, I hate named argumts in general. This situation is one of the few
places I've ever run into where I thought that they made any sense. This
type of situation is one of the few where having a bunc of parameters and
setting only a few makes much sense. So, I tend forget that the syntax that
you used here even exists, and I actively avoid it when I do remember that
it exists. Regardless, I was just pointing out what I'd done with dxml,
since it seems to match the use case here. I'm certainly not arguing that
it's always better. I don't think that I'd ever use a solution though that
encouraged using the {} syntax for initializing a struct, since I wish that
it wasn't even in the language.

- Jonathan M Davis



More information about the Digitalmars-d mailing list