www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - alias base._ctor is not as powerful as i thought

reply DavidL <Davidl 126.com> writes:
class base
{
	int j;
	this(char[] hello){}
	this(int k){}
	this(){}
}
class inherit:base
{
	int k;
	float myfloat;
	alias base._ctor _ctor;
	this(){}
	void show(){writefln("%d%d",k,j);}
}

void main()
{
	base uber = new inherit(3);
	(cast(inherit)uber).k=40;
         // assertion fails here
	assert((cast(inherit)uber).myfloat == float.nan);	// it's sad it's not NAN
}

Yet, after some though for a while, it's the correct behavior to force  
users to cast & thus
no RAII for derived class members. But this makes alias base._ctor _ctor;  
some what unusable
If there's any way of making it smarter would be nicer :D
like autogen wrapper for alias _ctor with

this(protocal)
{
    super.this
}

current behavior is a bit low level, and it could hide some problems, if  
users're not aware of
the current behavior.

-- 
使用 Opera 革命性的电子邮件客户程序: http://www.opera.com/mail/
Sep 08 2007
next sibling parent reply "Janice Caron" <caron800 googlemail.com> writes:
Doesn't assert(something == float.nan) /always/ fail?

Aren't you supposed to do isNan() or something?
Sep 08 2007
parent DavidL <Davidl 126.com> writes:
鍦 Sun, 09 Sep 2007 14:58:54 +0800锛孞anice Caron <caron800 googlemail.com>  
鍐欓亾:

 Doesn't assert(something == float.nan) /always/ fail?

 Aren't you supposed to do isNan() or something?

oops, i tested it with != float.nan, it passes, so i change it to the opposite :o yeah , i should test it with some Nan specific test operator, tough too lazy to take a look at spec.... -- 浣跨敤 Opera 闈╁懡鎬х殑鐢靛瓙閭欢瀹㈡埛绋嬪簭: http://www.opera.com/mail/
Sep 09 2007
prev sibling parent DavidL <Davidl 126.com> writes:
鍦 Sun, 09 Sep 2007 12:37:03 +0800锛孌avidL <Davidl 126.com> 鍐欓亾:

 class base
 {
 	int j;
 	this(char[] hello){}
 	this(int k){}
 	this(){}
 }
 class inherit:base
 {
 	int k;
 	float myfloat;
 	alias base._ctor _ctor;
 	this(){}
 	void show(){writefln("%d%d",k,j);}
 }

 void main()
 {
 	base uber = new inherit(3);
 	(cast(inherit)uber).k=40;
          // assertion fails here
 	assert((cast(inherit)uber).myfloat == float.nan);	// it's sad it's not  
 NAN
 }

 Yet, after some though for a while, it's the correct behavior to force  
 users to cast & thus
 no RAII for derived class members. But this makes alias base._ctor  
 _ctor; some what unusable
 If there's any way of making it smarter would be nicer :D
 like autogen wrapper for alias _ctor with

 this(protocal)
 {
     super.this
 }

 current behavior is a bit low level, and it could hide some problems, if  
 users're not aware of
 the current behavior.

:D the members are initialized. so the cast could be killed maybe ? then we can say byebye to trivial this(){super()} -- 浣跨敤 Opera 闈╁懡鎬х殑鐢靛瓙閭欢瀹㈡埛绋嬪簭: http://www.opera.com/mail/
Sep 09 2007