Thread overview
InstanceOf Template during runtime in a variant
Feb 03, 2014
Andre
Feb 03, 2014
Dicebot
Feb 03, 2014
Dicebot
Feb 03, 2014
Ali Çehreli
Feb 03, 2014
Andre
Feb 03, 2014
Dicebot
Aug 06, 2015
Gary Willoughby
February 03, 2014
Hi,

I want to check whether the value stored in
variant v is a type of Decimal during runtime.
Is there a nice way?

Kind regards
André

import std.variant;

struct Decimal(int scale, int precision){
	int _precision = precision;
	int _scale = scale;

	this(string value){/*...*/}
}

void main(){
	Variant v = Decimal!(10,2)("123.00");
}
February 03, 2014
No. Variant only stores TypeInfo for its current data and templated struct will have a totally different type for each set of template arguments. Their similarity exists only during compile-time.
February 03, 2014
(you can check for specific type via `v.type() ==
typeid(Decimal!(10,2))` though)
February 03, 2014
On 02/03/2014 10:15 AM, Andre wrote:
>
> I want to check whether the value stored in
> variant v is a type of Decimal during runtime.
> Is there a nice way?
>
> Kind regards
> André
>
> import std.variant;
>
> struct Decimal(int scale, int precision){
>      int _precision = precision;
>      int _scale = scale;

This is unrelated to your question but you don't need those members as the two template parameters 'scale' and 'precision' are available.

If you needed 'precision' and 'scale' be variables, then you probably don't want to make Decimal a template but I can't be sure from here. :)

>
>      this(string value){/*...*/}
> }
>
> void main(){
>      Variant v = Decimal!(10,2)("123.00");
> }

Ali

February 03, 2014
Am 03.02.2014 20:09, schrieb Ali Çehreli:
> On 02/03/2014 10:15 AM, Andre wrote:
>  >
>  > I want to check whether the value stored in
>  > variant v is a type of Decimal during runtime.
>  > Is there a nice way?
>  >
>  > Kind regards
>  > André
>  >
>  > import std.variant;
>  >
>  > struct Decimal(int scale, int precision){
>  >      int _precision = precision;
>  >      int _scale = scale;
>
> This is unrelated to your question but you don't need those members as
> the two template parameters 'scale' and 'precision' are available.
>
> If you needed 'precision' and 'scale' be variables, then you probably
> don't want to make Decimal a template but I can't be sure from here. :)
>
>  >
>  >      this(string value){/*...*/}
>  > }
>  >
>  > void main(){
>  >      Variant v = Decimal!(10,2)("123.00");
>  > }
>
> Ali
>

Thanks for the answers. Yes you are correct, the 2 members are superflous.
Btw. having std.decimal in the library would be really nice;)

Kind regards
André

February 03, 2014
On Monday, 3 February 2014 at 19:35:47 UTC, Andre wrote:
> Btw. having std.decimal in the library would be really nice;)
>
> Kind regards
> André

There is a proposal in Phobos review queue (http://wiki.dlang.org/Review_Queue) but its author does not seem to be active anymore so it moves nowhere.
August 06, 2015
On Monday, 3 February 2014 at 19:47:56 UTC, Dicebot wrote:
> On Monday, 3 February 2014 at 19:35:47 UTC, Andre wrote:
>> Btw. having std.decimal in the library would be really nice;)
>>
>> Kind regards
>> André
>
> There is a proposal in Phobos review queue (http://wiki.dlang.org/Review_Queue) but its author does not seem to be active anymore so it moves nowhere.

I notice he is active again and the repo has been currently worked on. Can someone give him a nudge and get this process moving?