digitalmars.D.learn - Help with specific template function
- Vladimirs Nordholm (18/18) Mar 25 2018 Hello Dlang community!
- Vladimirs Nordholm (9/13) Mar 25 2018 I found a solution which satisfies my needs :)
- aliak (8/22) Mar 25 2018 FYI there's also
- Vladimirs Nordholm (13/38) Mar 25 2018 Thanks Ali, I had not thought about chars, bools, etc.
- rumbu (3/14) Mar 25 2018 isNumeric applies to a type, not to a variable => IsNumeric!X
- arturg (4/5) Mar 25 2018 use the type not the variables:
- Vladimirs Nordholm (3/6) Mar 26 2018 Ah, missed that. Thanks a bunch!
Hello Dlang community!
I need help in creating a template function which would look like 
the following pseudo-code:
     void foo(<a number>, <also a number>, <required arg>, 
<optional arg>...);
     // would be used as `void foo(x, y, arg1, arg2, ..., argN)`
     // valid examples:
     foo(5.332, 1, "a string", 123, "42");
     foo(3, 44, false);
     // invalid examples:
     foo(1, 1);
     foo(3, "stringers", "arg");
The underlying problems are:
* How do I ensure the two first arguments (used as coordinates) 
are types of numbers (all kinds: ints, floats, reals, etc.)
* At least one argument is passed after the coordinates
Best regards,
Vladimris Nordholm
 Mar 25 2018
On Sunday, 25 March 2018 at 18:24:37 UTC, Vladimirs Nordholm wrote:The underlying problems are: * How do I ensure the two first arguments (used as coordinates) are types of numbers (all kinds: ints, floats, reals, etc.) * At least one argument is passed after the coordinatesI found a solution which satisfies my needs :) void foo(X, Y, Args...)(X x, Y y, Args args) if (__traits(isArithmetic, x) && __traits(isArithmetic, y) && args.length >= 1) { // ... }
 Mar 25 2018
On Sunday, 25 March 2018 at 19:06:14 UTC, Vladimirs Nordholm wrote:On Sunday, 25 March 2018 at 18:24:37 UTC, Vladimirs Nordholm wrote:FYI there's also https://dlang.org/library/std/traits/is_numeric.html incase you haven't see it -takes in to account bools and chars, which depending on your usecase you may not want to count as arithmetic. Cheers - AliThe underlying problems are: * How do I ensure the two first arguments (used as coordinates) are types of numbers (all kinds: ints, floats, reals, etc.) * At least one argument is passed after the coordinatesI found a solution which satisfies my needs :) void foo(X, Y, Args...)(X x, Y y, Args args) if (__traits(isArithmetic, x) && __traits(isArithmetic, y) && args.length >= 1) { // ... }
 Mar 25 2018
On Sunday, 25 March 2018 at 21:31:16 UTC, aliak wrote:On Sunday, 25 March 2018 at 19:06:14 UTC, Vladimirs Nordholm wrote:Thanks Ali, I had not thought about chars, bools, etc. However I do not understand how to use that with my arguments. Eg. I would expect to do something like: void foo(X, Y, Args...)(X x, Y y, Args args) if(isNumeric!(x) && isNumeric!(y) && args.length >= 1) { // ... } gives the error template instance isNumeric!(x) does not match template declaration isNumeric(T) How would I resolve this issue?On Sunday, 25 March 2018 at 18:24:37 UTC, Vladimirs Nordholm wrote:FYI there's also https://dlang.org/library/std/traits/is_numeric.html incase you haven't see it -takes in to account bools and chars, which depending on your usecase you may not want to count as arithmetic. Cheers - AliThe underlying problems are: * How do I ensure the two first arguments (used as coordinates) are types of numbers (all kinds: ints, floats, reals, etc.) * At least one argument is passed after the coordinatesI found a solution which satisfies my needs :) void foo(X, Y, Args...)(X x, Y y, Args args) if (__traits(isArithmetic, x) && __traits(isArithmetic, y) && args.length >= 1) { // ... }
 Mar 25 2018
On Monday, 26 March 2018 at 06:40:34 UTC, Vladimirs Nordholm 
wrote:
 However I do not understand how to use that with my arguments. 
 Eg. I would expect to do something like:
     void foo(X, Y, Args...)(X x, Y y, Args args)
     if(isNumeric!(x) && isNumeric!(y) && args.length >= 1)
     {
         // ...
     }
 gives the error
     template instance isNumeric!(x) does not match template 
 declaration isNumeric(T)
 How would I resolve this issue?
isNumeric applies to a type, not to a variable => IsNumeric!X
 Mar 25 2018
On Monday, 26 March 2018 at 06:40:34 UTC, Vladimirs Nordholm wrote:How would I resolve this issue?use the type not the variables: isNumeric!X && isNumeric!Y
 Mar 25 2018
On Monday, 26 March 2018 at 06:48:45 UTC, rumbu wrote:isNumeric applies to a type, not to a variable => IsNumeric!XOn Monday, 26 March 2018 at 06:51:48 UTC, arturg wrote:use the type not the variables: isNumeric!X && isNumeric!YAh, missed that. Thanks a bunch!
 Mar 26 2018








 
  
  
 
 rumbu <rumbu rumbu.ro>
 rumbu <rumbu rumbu.ro> 