Style question
Namespace
rswhite4 at googlemail.com
Thu Jul 11 11:22:10 PDT 2013
I have a style question, because a friend of mine has a similar
problem currently and I have no good advice for him.
Let's assume we have this classes:
----
class MyClass {
public:
enum A {
Foo = 0,
Bar = 1
}
private:
A _a;
public:
this(A a) {
this._a = a;
}
void test1() {
MyStaticClass.test2(this._a);
}
}
//----
enum B {
Foo = 0,
Bar = 1
}
final abstract class MyStaticClass {
public:
static void test2(B b) { }
}
void main() {
}
----
Prints: Error: function enum_problem.MyStaticClass.test2 (B b) is
not callable using argument types (A)
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?
More information about the Digitalmars-d-learn
mailing list