How std.getopt would look with type functions
Stefan Koch
uplink.coder at googlemail.com
Sun May 24 00:15:07 UTC 2020
Just now I thought about one of the things that always irked me
about variadic template parameter lists. The lack of type
checking.
And the wired errors which are getting caught far too late in the
template instance stack.
Let's take std.getopt as an example.
The first example givin in it's docs is this
auto helpInformation = getopt(
args,
"length", &length, // numeric
"file", &data, // string
"verbose", &verbose, // flag
"color", "Information about this color", &color); // enum
The pattern is clear it's a tuple representing {name,
pointer_to_var} excpect when the second parameter is not a
pointer but a string in which case it's {name, helptext pointer,
to var} or maybe just forgot to pass another pointer to a var in
there ? And "Information about this color" is just a very long
option name?
type functions can easily fix that.
Since they also introduce the ability to store references to
symbols in structs.
struct arg { string name; alias var; string help = null }
getOps(args, arg("length", length),
arg("file", data),
arg("verbose", verbose),
arg("color", color, "Information about this color")
);
I argue that this is firstly more type safe.
And secondly looks much nicer.
getopt could be defined as getopt(string args, arg[] args...)
and one could leave the fiddling to detect how to interpret which
part of the argument list
out of the getopt implementation.
More information about the Digitalmars-d
mailing list