www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Is this the proper way to do it?

reply Jack <jckj33 gmail.com> writes:
I have a base class A, where I make specific operator depending 
on the derived class type. Currently I'm using something like 
this:

c is a class derived from A
bool shouldDoX = (cast(X)c) !is null || (cast(Y)c) !is null || 
(cast(K)c) !is null ... ;

as the number of cast(C) !is null is growing, I'm afraid of this 
being a inelegant or even poor performance approach. How would 
you do that?
Feb 12 2021
next sibling parent reply mw <mingwu gmail.com> writes:
On Saturday, 13 February 2021 at 05:52:34 UTC, Jack wrote:
 I have a base class A, where I make specific operator depending 
 on the derived class type. Currently I'm using something like 
 this:

 c is a class derived from A
 bool shouldDoX = (cast(X)c) !is null || (cast(Y)c) !is null || 
 (cast(K)c) !is null ... ;

 as the number of cast(C) !is null is growing, I'm afraid of 
 this being a inelegant or even poor performance approach. How 
 would you do that?
Isn't that what virtual function is designed for? ``` class Base { bool shouldDoX() {return false;} } class Derived: Base { bool shouldDoX() {return true;} } class Derived2: Derived { bool shouldDoX() {return false;} } ... ```
Feb 12 2021
parent Jack <jckj33 gmail.com> writes:
On Saturday, 13 February 2021 at 07:08:58 UTC, mw wrote:
 On Saturday, 13 February 2021 at 05:52:34 UTC, Jack wrote:
 I have a base class A, where I make specific operator 
 depending on the derived class type. Currently I'm using 
 something like this:

 c is a class derived from A
 bool shouldDoX = (cast(X)c) !is null || (cast(Y)c) !is null || 
 (cast(K)c) !is null ... ;

 as the number of cast(C) !is null is growing, I'm afraid of 
 this being a inelegant or even poor performance approach. How 
 would you do that?
Isn't that what virtual function is designed for? ``` class Base { bool shouldDoX() {return false;} } class Derived: Base { bool shouldDoX() {return true;} } class Derived2: Derived { bool shouldDoX() {return false;} } ... ```
sounds a better approach, I ended up using this. Lots of cast(X), cast(Y), etc is probably slow and gets messy with time.
Feb 22 2021
prev sibling next sibling parent reply Rumbu <rumbu rumbu.ro> writes:
On Saturday, 13 February 2021 at 05:52:34 UTC, Jack wrote:
 I have a base class A, where I make specific operator depending 
 on the derived class type. Currently I'm using something like 
 this:

 c is a class derived from A
 bool shouldDoX = (cast(X)c) !is null || (cast(Y)c) !is null || 
 (cast(K)c) !is null ... ;

 as the number of cast(C) !is null is growing, I'm afraid of 
 this being a inelegant or even poor performance approach. How 
 would you do that?
Option 1, reverse the condition, && op will shortcut boolean conditions bool shouldNotDoX = cast(X)c && cast(Y)c && cast(K)c && ... Option 2, reverse the condition by testing the classes that are not supposed to "do" it, if you have less classes in that category. bool shoudldNotDoX = cast(P)c || cast(Q)c
Feb 13 2021
parent Jack <jckj33 gmail.com> writes:
On Saturday, 13 February 2021 at 09:54:28 UTC, Rumbu wrote:
 On Saturday, 13 February 2021 at 05:52:34 UTC, Jack wrote:
 I have a base class A, where I make specific operator 
 depending on the derived class type. Currently I'm using 
 something like this:

 c is a class derived from A
 bool shouldDoX = (cast(X)c) !is null || (cast(Y)c) !is null || 
 (cast(K)c) !is null ... ;

 as the number of cast(C) !is null is growing, I'm afraid of 
 this being a inelegant or even poor performance approach. How 
 would you do that?
Option 1, reverse the condition, && op will shortcut boolean conditions bool shouldNotDoX = cast(X)c && cast(Y)c && cast(K)c && ... Option 2, reverse the condition by testing the classes that are not supposed to "do" it, if you have less classes in that category. bool shoudldNotDoX = cast(P)c || cast(Q)c
I ended up using virtual functions, this cast and conditions would get too big, ugly and messy with time
Feb 22 2021
prev sibling parent reply frame <frame86 live.com> writes:
On Saturday, 13 February 2021 at 05:52:34 UTC, Jack wrote:
 I have a base class A, where I make specific operator depending 
 on the derived class type. Currently I'm using something like 
 this:

 c is a class derived from A
 bool shouldDoX = (cast(X)c) !is null || (cast(Y)c) !is null || 
 (cast(K)c) !is null ... ;

 as the number of cast(C) !is null is growing, I'm afraid of 
 this being a inelegant or even poor performance approach. How 
 would you do that?
I would just use an (empty) interface on that classes and do test for that.
Feb 13 2021
parent Jack <jckj33 gmail.com> writes:
On Saturday, 13 February 2021 at 19:40:43 UTC, frame wrote:
 On Saturday, 13 February 2021 at 05:52:34 UTC, Jack wrote:
 I have a base class A, where I make specific operator 
 depending on the derived class type. Currently I'm using 
 something like this:

 c is a class derived from A
 bool shouldDoX = (cast(X)c) !is null || (cast(Y)c) !is null || 
 (cast(K)c) !is null ... ;

 as the number of cast(C) !is null is growing, I'm afraid of 
 this being a inelegant or even poor performance approach. How 
 would you do that?
I would just use an (empty) interface on that classes and do test for that.
i did consider that too but ended up with virtual functions
Feb 22 2021