www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - How to pass default arguments in a compile time sequence?

reply Victor Porton <porton narod.ru> writes:
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?
Mar 01 2019
parent reply Dukc <ajieskola gmail.com> writes:
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.
Mar 01 2019
parent Victor Porton <porton narod.ru> writes:
On Friday, 1 March 2019 at 18:52:58 UTC, Dukc wrote:
 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.
To infer the value of the field from the literal seems an interesting idea. However I was more inclined to do something like: mixin StructParams!("S", int, "x", float, "y", 2.1f); This seems more readable. Isn't it?
 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.
I am going to do this work thoroughly and quality. So I am willing to invest time into this task.
Mar 01 2019