contravariant argument types: wanna?

Michel Fortin michel.fortin at michelf.com
Wed Sep 23 09:59:45 PDT 2009


On 2009-09-23 05:33:34 -0400, Walter Bright <newshound1 at digitalmars.com> said:

> Andrei Alexandrescu wrote:
>> Today D does not support contravariant arguments, but Walter told me 
>> once he'd be quite willing to implement them. It is definitely the 
>> right thing to do, but Walter would want to see a compelling example 
>> before getting to work.
> 
> The stumbling block to contravariant parameters (not arguments) is how 
> it throws a monkey wrench into the overloading rules. The problem is 
> deciding if a function overrides or overloads, when its parameter types 
> are different.
> 
> The current rule is simple: if the parameter types are identical, it 
> overrides. If they are not identical, it overloads.

Yes, that rule is simple, even simplistic. If keeping this rule simple 
forces us to write boilerplate code where it would otherwise not be 
necessary, then it justs shifts complexity to the one who writes the 
code. Case in point:

	class Serializer {
		abstract byte[] serialize(SerializableObject o);
	}

	class UniversalSerializer : Serializer {
		byte[] serialize(Object o) { ... }

		// boilerplate code to implement Serializer.serialize
		override byte[] serialize(SerializableObject o) { return 
serialize(cast(Object)o); }
	}

Unfortunately, people might expect being able to mix classes and 
interfaces with this, which isn't easily reaslizable. But I believe 
mixing objects and interfaces is a problem that should be solved in 
more general way.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/




More information about the Digitalmars-d mailing list