www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - InstanceOf Template during runtime in a variant

reply Andre <andre s-e-a-p.de> writes:
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");
}
Feb 03 2014
next sibling parent reply "Dicebot" <public dicebot.lv> writes:
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.
Feb 03 2014
parent "Dicebot" <public dicebot.lv> writes:
(you can check for specific type via `v.type() ==
typeid(Decimal!(10,2))` though)
Feb 03 2014
prev sibling parent reply =?UTF-8?B?QWxpIMOHZWhyZWxp?= <acehreli yahoo.com> writes:
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
Feb 03 2014
parent reply Andre <andre s-e-a-p.de> writes:
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é
Feb 03 2014
parent reply "Dicebot" <public dicebot.lv> writes:
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.
Feb 03 2014
parent "Gary Willoughby" <dev nomad.so> writes:
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?
Aug 06 2015