Thread overview
Check if a variadic argument is numeric?
Jul 06, 2017
Hamborg
Jul 06, 2017
Ali Çehreli
Jul 06, 2017
Hamborg
Jul 06, 2017
Andrea Fontana
Jul 06, 2017
Hamborg
Jul 06, 2017
Andrea Fontana
Jul 06, 2017
Adam D. Ruppe
July 06, 2017
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.
July 06, 2017
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

July 06, 2017
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"

July 06, 2017
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
July 06, 2017
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... :(
July 06, 2017
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
July 06, 2017
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.