How to pass default arguments in a compile time sequence?
Dukc
ajieskola at gmail.com
Fri Mar 1 18:52:58 UTC 2019
On Friday, 1 March 2019 at 17:55:50 UTC, Victor Porton wrote:
> I have a string mixin, such that
>
> mixin StructParams!("S", int, "x", float, "y");
>
> creates
>
> struct S {
> struct Regular {
> int x;
> float y;
> }
> struct WithDefaults {
> Nullable!int x;
> Nullable!float y;
> }
> }
>
> Now I want to enhance StructParams to be able to pass default
> initializers (like `float y = 2.1;`). What "format" for the
> template arguments would you suggest to be able to either pass
> or not to pass default initializers (like 2.1) for each of the
> fields of the structs?
One where you pass the default value in place of where you would
declare the type if you don't want to pass the value. So that the
above declaration with float y having a default value would be:
mixin StructParams!("S", int, "x", 2.1f, "y");
Define StructParams template so that infer type of y from type of
the expression.
But I suggest the one above only if you are willing to put a good
deal of thought in implementing StructParams template. It won't
be the easiest of tasks. If you're doing something
quick-and-dirty, I would probably just define a function that
constructs S with appopriate "default" values.
More information about the Digitalmars-d
mailing list