Re: InstanceOf Template during runtime in a variant

2015-08-06 Thread Gary Willoughby via Digitalmars-d-learn
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

Re: InstanceOf Template during runtime in a variant

2014-02-03 Thread Dicebot
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.

Re: InstanceOf Template during runtime in a variant

2014-02-03 Thread Andre
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 prec

Re: InstanceOf Template during runtime in a variant

2014-02-03 Thread 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

Re: InstanceOf Template during runtime in a variant

2014-02-03 Thread Dicebot
(you can check for specific type via `v.type() == typeid(Decimal!(10,2))` though)

Re: InstanceOf Template during runtime in a variant

2014-02-03 Thread Dicebot
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.

InstanceOf Template during runtime in a variant

2014-02-03 Thread Andre
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){/*...

Re: InstanceOf

2013-06-24 Thread Lemonfiend
On Monday, 24 June 2013 at 15:46:05 UTC, Steven Schveighoffer wrote: On Sun, 23 Jun 2013 11:29:10 -0400, Lemonfiend wrote: On Sunday, 23 June 2013 at 15:15:16 UTC, Jacob Carlborg wrote: On 2013-06-23 13:26, Lemonfiend wrote: foreach (I i; array) { if (B b = cast(B) i) { ... } } Thanks a

Re: InstanceOf

2013-06-24 Thread Steven Schveighoffer
On Sun, 23 Jun 2013 11:29:10 -0400, Lemonfiend wrote: On Sunday, 23 June 2013 at 15:15:16 UTC, Jacob Carlborg wrote: On 2013-06-23 13:26, Lemonfiend wrote: foreach (I i; array) { if (B b = cast(B) i) { ... } } Thanks all 3 of you for the quick and identical answers. :) It had not occurr

Re: InstanceOf

2013-06-23 Thread Jacob Carlborg
On 2013-06-23 17:29, Lemonfiend wrote: Using the .classinfo is what I looked at before asking here. However, according to the specs: ".classinfo applied to an interface gives the information for the interface, not the class it might be an instance of." So the i.classinfo and B.classinfo would be

Re: InstanceOf

2013-06-23 Thread Lemonfiend
; So the i.classinfo and B.classinfo would be different? You can also hide the cast in a function if you want to be a bit more clear of the intent: T instanceOf (T) (Object value) { return cast(T) value); } if (i.instanceOf!(B)) { } This is indeed what I did :)

Re: InstanceOf

2013-06-23 Thread Jacob Carlborg
for. You can also hide the cast in a function if you want to be a bit more clear of the intent: T instanceOf (T) (Object value) { return cast(T) value); } if (i.instanceOf!(B)) { } -- /Jacob Carlborg

Re: InstanceOf

2013-06-23 Thread Timon Gehr
o is an instance of a class B use a cast" It does a bit inelegant to me.. Or are casts simply extremely cheap? Casts are as cheap as testing whether an object is an instance of a certain class. Having the cast evaluate to a nullable reference is certainly more elegant than having inst

Re: InstanceOf

2013-06-23 Thread Lemonfiend
foreach (I i; array) { if (B b = cast(B) i) { ... } } Thanks all 3 of you for the quick and identical answers. :) It had not occurred to me to use a cast for this, but indeed the language ref says the same: "In order to determine if an object o is an instance of a class B use a cast" It

Re: InstanceOf

2013-06-23 Thread Namespace
of a specific type (a la instanceOf), and I can't figure out how to do it. Could anyone help me out? [code] interface I { void update(); void write(); } class A : I { int n; void update() { n++; }

Re: InstanceOf

2013-06-23 Thread Chris Cain
On Sunday, 23 June 2013 at 11:04:59 UTC, Lemonfiend wrote: ... void writeAll() { foreach(elem; array) elem.write(); } void writeB() { // Only call .write on B's // How do I get the B's

Re: InstanceOf

2013-06-23 Thread Artur Skawina
On 06/23/13 13:04, Lemonfiend wrote: > void writeAll() > { > foreach(elem; array) > elem.write(); > } > > void writeB() > { > // Only call .write on B's > // How do I get the B's from the array of I's? > } void writeB() {

InstanceOf

2013-06-23 Thread Lemonfiend
I'm trying to create a fairly generic component system, where an object iterates over a bunch of other objects that all implement a certain interface. And this all works fine, however, I would also like to be able to get objects of a specific type (a la instanceOf), and I can't figu

Re: instanceof?

2012-08-01 Thread Jacob Carlborg
On 2012-08-02 05:06, Jonathan M Davis wrote: if(cast(Class)obj) is the canonical way to do it. Or, to use the value after the cast: if (auto foo = cast(Class) obj) // use foo -- /Jacob Carlborg

Re: instanceof?

2012-08-01 Thread Jonathan M Davis
On Thursday, August 02, 2012 03:59:01 Maxime Chevalier wrote: > Getting started with D. I've been doing alot of googling trying > to find out what's the best way to check if an object is an > instance of some class. So far, I found you could do: > > if (typeid(obj) == typeid(Class)) doSomething();

Re: instanceof?

2012-08-01 Thread Adam D. Ruppe
On Thursday, 2 August 2012 at 01:59:02 UTC, Maxime Chevalier wrote: These both seem a little clumsy, however. Is there a better way to do this? I don't know if it is the best way, but I use the cast myself, though I like using a little wrapper: T instanceof(T)(Object o) if(is(T ==

instanceof?

2012-08-01 Thread Maxime Chevalier
Getting started with D. I've been doing alot of googling trying to find out what's the best way to check if an object is an instance of some class. So far, I found you could do: if (typeid(obj) == typeid(Class)) doSomething(); or: if (cast(Class)obj) doSomething(); These both seem a little c