On 02/12/2012 09:56 PM, Mike Wey wrote:
On 02/12/2012 09:08 PM, Walter Bright wrote:

It's a Base, not an Object. It can be converted to an Object.

Doesn't that break Polymorphism?

You should be able to use Base as if it is an Object without a cast.


_______________________________________________
dmd-beta mailing list
[email protected]
http://lists.puremagic.com/mailman/listinfo/dmd-beta

According to the documentation a == b is rewritten to .object.opEquals(a, b); if a and b are both objects. And while .object.opEquals(new Base(), new Base()); compiles without an error, and gives the expected result at runtime. using == is an compiletime error when a opCast is supplied without adding one for Object.

Base being derived from Object there would be no need for a cast when you want to use it as an Object.

Adding an opCast overload for Object solves the compiletime error but, the added opCast doesn't do anything:

Base opCast(T)()
    if ( is(T == Object) )
{
    return this;
}

Yes i used Base as the return type, that works because base is derived from Object.

Is the behavior of the beta really correct?
module test;

class Base
{
	override bool opEquals(Object obj)
	{
		return true;
	}
	
	//Base opCast(T)()
	//	if ( is(T == Object) )
	//{
	//	return this;
	//}

	T opCast(T : Base)()
	{
		return this;
	}
}

void main()
{
	// Works.
	assert(.object.opEquals(new Base(), new Base()));

	//Error: template instance opCast!(Object) opCast!(Object)
	//       does not match template declaration opCast(T : Base).
	assert(new Base() == new Base());
}
_______________________________________________
dmd-beta mailing list
[email protected]
http://lists.puremagic.com/mailman/listinfo/dmd-beta

Reply via email to