Re: InstanceOf Template during runtime in a variant
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?
InstanceOf Template during runtime in a variant
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); }
Re: InstanceOf Template during runtime in a variant
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.
Re: InstanceOf Template during runtime in a variant
(you can check for specific type via `v.type() == typeid(Decimal!(10,2))` though)
Re: InstanceOf Template during runtime in a variant
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
Re: InstanceOf Template during runtime in a variant
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é
Re: InstanceOf Template during runtime in a variant
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
On Sun, 23 Jun 2013 11:29:10 -0400, Lemonfiend le...@fie.nd 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 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 does a bit inelegant to me.. Or are casts simply extremely cheap? You can do something like this as well: if (i.classinfo is B.classinfo) { } But doing the cast is more efficient if you want to use the object of as the type you're checking for. 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 different? 1. Use typeid(i), not i.classinfo, classinfo is old-style. 2. Yes, typeid(i) will give you interface class info, or maybe even derived interface class info. It's a simple indirection, whereas a cast must go through a pointer offset stored in the interface typeinfo in order to get true class info. 3. typeid(i) on one DLL may be different than typeid(i) on another. It is not valid to compare the references directly On 2, see test code: http://dpaste.dzfl.pl/97f5866d -Steve
Re: InstanceOf
On Monday, 24 June 2013 at 15:46:05 UTC, Steven Schveighoffer wrote: On Sun, 23 Jun 2013 11:29:10 -0400, Lemonfiend le...@fie.nd 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 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 does a bit inelegant to me.. Or are casts simply extremely cheap? You can do something like this as well: if (i.classinfo is B.classinfo) { } But doing the cast is more efficient if you want to use the object of as the type you're checking for. 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 different? 1. Use typeid(i), not i.classinfo, classinfo is old-style. 2. Yes, typeid(i) will give you interface class info, or maybe even derived interface class info. It's a simple indirection, whereas a cast must go through a pointer offset stored in the interface typeinfo in order to get true class info. 3. typeid(i) on one DLL may be different than typeid(i) on another. It is not valid to compare the references directly On 2, see test code: http://dpaste.dzfl.pl/97f5866d -Steve This method would not work for my example, since it would get the interface, not a class. But if I were to maintain an array of some base class instead, it would. Very interesting! Thanks.
InstanceOf
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 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++; } void write() { writeln(n); } } class B : I { int m; void update() { m--; } void write() { writeln(m); } } class C { I[] array; void addElem(I elem) { array ~= elem; } void loop() { foreach(elem; array) elem.update(); } 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 main() { C c = new C(); c.addElem(new A()); c.addElem(new B()); c.loop(); c.writeAll(); c.writeB(); // This is the problem } [/code]
Re: InstanceOf
On Sunday, 23 June 2013 at 11:04:59 UTC, Lemonfiend wrote: 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 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++; } void write() { writeln(n); } } class B : I { int m; void update() { m--; } void write() { writeln(m); } } class C { I[] array; void addElem(I elem) { array ~= elem; } void loop() { foreach(elem; array) elem.update(); } 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 main() { C c = new C(); c.addElem(new A()); c.addElem(new B()); c.loop(); c.writeAll(); c.writeB(); // This is the problem } [/code] foreach (I i; array) { if (B b = cast(B) i) { ... } }
Re: InstanceOf
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 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 does a bit inelegant to me.. Or are casts simply extremely cheap? You can do something like this as well: if (i.classinfo is B.classinfo) { } But doing the cast is more efficient if you want to use the object of as the type you're checking 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
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 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 does a bit inelegant to me.. Or are casts simply extremely cheap? You can do something like this as well: if (i.classinfo is B.classinfo) { } But doing the cast is more efficient if you want to use the object of as the type you're checking for. 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 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
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 different? That might be the case, didn't think of that. In that case you need casts anyway. -- /Jacob Carlborg
Re: instanceof?
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
instanceof?
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 clumsy, however. Is there a better way to do this?
Re: instanceof?
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 == class)) { return cast(T) o; } Then, you can say if(object.instanceof!SomeClass) { // use } or if(auto some_class = object.instanceof!SomeClass) { // use some_class }
Re: instanceof?
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(); or: if (cast(Class)obj) doSomething(); These both seem a little clumsy, however. Is there a better way to do this? if(cast(Class)obj) is the canonical way to do it. - Jonathan M Davis