Thread overview
Is this the proper way to do it?
Feb 13, 2021
Jack
Feb 13, 2021
mw
Feb 22, 2021
Jack
Feb 13, 2021
Rumbu
Feb 22, 2021
Jack
Feb 13, 2021
frame
Feb 22, 2021
Jack
February 13, 2021
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?
February 13, 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;}
}

...

```
February 13, 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


February 13, 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.
February 22, 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:
>> 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.
February 22, 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 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


February 22, 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 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