Style question

Regan Heath regan at netmail.co.nz
Fri Jul 12 03:23:09 PDT 2013


On Thu, 11 Jul 2013 19:22:10 +0100, Namespace <rswhite4 at googlemail.com>  
wrote:
> 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?

If A and B are supposed to be/represent the same thing, then they should  
be the same enumeration - move them/it into a separate module and import  
into both MyClass and MyStaticClass.

If they're supposed to be different, then you treat them as separate types  
and either cast, or range check then cast.  So, add a uint constructor to  
MyStaticClass (in addition to the existing B constructor).  Have the uint  
constructor range check the value using assert, or exceptions, then cast  
valid values to B internally.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d-learn mailing list