Style question

Simen Kjaeraas simen.kjaras at gmail.com
Thu Jul 11 11:49:18 PDT 2013


On 2013-07-11, 20:22, Namespace wrote:

> What should he do?
>
> As far as I can see he has 3 options:
> 1. An external file with the enum information. Both classes would import  
> it and could use the same enum. But he cannot change the API, so this is  
> no real option.
>
> 2. Change test1 into this:
> ----
> void test1() {
> 	B b = cast(B) this.a;
> 	MyStaticClass.test2(b);
> }
> ----
> This works fine. But is it safe? And is it good style?
> And how is this cast converted? Is it cheap?
>
> 3. Change test2 so that it accepts (even) (u)int. But then he lose the  
> Type safety.
>
> Does anyone have any advice?

It seems to me that MyClass has access to MyStaticClass, and thus should
also have access to B. If this is the case, why is MyClass using an A
instead of a B?

One option might be to use alias A = B; This would ensure the two enums
are always the same.

Last ditch, I would say option #2 is the best. It's not safe, in that
if one enum changes and the other does not, the cast will blithely
ignore that and use the same uint value. It is however efficient, as it
is a simple reinterpretation of the bits.

-- 
Simen


More information about the Digitalmars-d-learn mailing list