named arguments (C++) - Something D could learn from
Simen Kjærås
simen.kjaras at gmail.com
Wed Dec 19 15:48:06 UTC 2018
On Tuesday, 18 December 2018 at 18:23:20 UTC, aliak wrote:
> Woah... that's quite neat.
>
> I feel like this is how std.typecons.Flag should have been
> made!!
>
> I played around with it for a bit, and if there was a way to
> get some better error messages then that could be quite nice to
> use! - i dunno, maybe through some static assert on the "string
> names"s being passed in or something...
Thanks, but it has some serious drawbacks - like this:
void fun(Args!byte.foo foo) {}
unittest {
fun(args.foo = 3); // function foo.fun(Arg!("foo", byte) foo)
is not callable using argument types (Arg!("foo", int))
}
For a limited use case like std.typecons.Flag, it could work
pretty well, though:
struct Flag {
alias opDispatch(string name) = FlagImpl!name;
}
template FlagImpl(string name) {
// mixin to embetter type name.
mixin(`struct `~name~` {
bool value;
alias value this;
static typeof(this) opAssign(bool value) {
return typeof(this)(value);
}
}`);
mixin("alias FlagImpl = "~name~";");
}
void fun(Flag.exclusive exclusive) {
assert(exclusive);
}
unittest {
fun(Flag.exclusive = true);
}
--
Simen
More information about the Digitalmars-d
mailing list