digitalmars.D.learn - Is this the proper way to do it?
- Jack (9/9) Feb 12 2021 I have a base class A, where I make specific operator depending
- mw (14/23) Feb 12 2021 Isn't that what virtual function is designed for?
- Jack (3/28) Feb 22 2021 sounds a better approach, I ended up using this. Lots of cast(X),
- Rumbu (8/17) Feb 13 2021 Option 1, reverse the condition, && op will shortcut boolean
- Jack (3/22) Feb 22 2021 I ended up using virtual functions, this cast and conditions
- frame (3/12) Feb 13 2021 I would just use an (empty) interface on that classes and do test
- Jack (2/16) Feb 22 2021 i did consider that too but ended up with virtual functions
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
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
On Saturday, 13 February 2021 at 07:08:58 UTC, mw wrote:On Saturday, 13 February 2021 at 05:52:34 UTC, Jack wrote:sounds a better approach, I ended up using this. Lots of cast(X), cast(Y), etc is probably slow and gets messy with time.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 22 2021
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
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 ended up using virtual functions, this cast and conditions would get too big, ugly and messy with timeI 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 22 2021
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
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 did consider that too but ended up with virtual functionsI 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 22 2021