www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Check if a variadic argument is numeric?

reply Hamborg <fsdfsd gmail.com> writes:
I can test for exactly what what the args are with (_arguments[i] 
== typeid(int)) but if I just want to know if it's numeric and 
can pull it out as a double what should I do? I don't really want 
to test for int, uint, byte, float, etc individually.
Jul 05
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 07/05/2017 11:26 PM, Hamborg wrote:
 I can test for exactly what what the args are with (_arguments[i] ==
 typeid(int)) but if I just want to know if it's numeric and can pull it
 out as a double what should I do? I don't really want to test for int,
 uint, byte, float, etc individually.
If it exists, it would be in std.traits or __traits. In this case it's std.traits.isNumeric. :) https://dlang.org/phobos/std_traits.html#isNumeric Ali
Jul 06
parent reply Hamborg <fsdfsd gmail.com> writes:
On Thursday, 6 July 2017 at 07:11:01 UTC, Ali Çehreli wrote:
 On 07/05/2017 11:26 PM, Hamborg wrote:
 I can test for exactly what what the args are with 
 (_arguments[i] ==
 typeid(int)) but if I just want to know if it's numeric and 
 can pull it
 out as a double what should I do? I don't really want to test 
 for int,
 uint, byte, float, etc individually.
If it exists, it would be in std.traits or __traits. In this case it's std.traits.isNumeric. :) https://dlang.org/phobos/std_traits.html#isNumeric Ali
But how would I check _arguments[i]? When I do (isNumeric!_arguments[i]) I get an error saying: "Error: template instance isNumeric!(_arguments) does not match template declaration isNumeric(T)" or when I do (isNumeric!(_arguments[i])) I get this error: "Error: variable _arguments cannot be read at compile time"
Jul 06
parent reply Andrea Fontana <nospam example.com> writes:
On Thursday, 6 July 2017 at 08:22:44 UTC, Hamborg wrote:

 But how would I check _arguments[i]?

 When I do (isNumeric!_arguments[i]) I get an error saying:
 "Error: template instance isNumeric!(_arguments) does not match 
 template declaration isNumeric(T)"

 or when I do (isNumeric!(_arguments[i])) I get this error:
 "Error: variable _arguments cannot be read at compile time"
Try with: isNumeric!(typeof(_arguments[i])); Andrea
Jul 06
parent reply Hamborg <fsdfsd gmail.com> writes:
On Thursday, 6 July 2017 at 08:35:05 UTC, Andrea Fontana wrote:
 On Thursday, 6 July 2017 at 08:22:44 UTC, Hamborg wrote:

 But how would I check _arguments[i]?

 When I do (isNumeric!_arguments[i]) I get an error saying:
 "Error: template instance isNumeric!(_arguments) does not 
 match template declaration isNumeric(T)"

 or when I do (isNumeric!(_arguments[i])) I get this error:
 "Error: variable _arguments cannot be read at compile time"
Try with: isNumeric!(typeof(_arguments[i])); Andrea
That compiles, but it returns false when I pass in a numeric value... :(
Jul 06
parent reply Andrea Fontana <nospam example.com> writes:
On Thursday, 6 July 2017 at 12:15:56 UTC, Hamborg wrote:
 That compiles, but it returns false when I pass in a numeric 
 value... :(
I think you should provide a code snippet then. I think I missed something about your question. Andrea
Jul 06
parent Adam D. Ruppe <destructionator gmail.com> writes:
On Thursday, 6 July 2017 at 12:26:11 UTC, Andrea Fontana wrote:
 I think you should provide a code snippet then. I think I 
 missed something about your question.
You did - this question is about a runtime variadic which gives you an array of TypeInfo objects, but all the answers are giving compile time things. None of them will work since the static type is some subclass of TypeInfo. There is no method in the interface to do what the OP wants. You'll have to do a list of comparisons.... or hack druntime, it is fairly easy to add such a method... but that's not a realistic solution either. Best option would perhaps be switching to a compile time variadic template.
Jul 06