This project is read-only.

Covariant return type overrides


There is a really common pattern that pops up over and over when using immutable classes since every mutation method returns a new instance. Dealing with the builders is even worse.

The result is a lot of casting or liberal use of new <methodname>.

It would be really nice if a derived class could override base class methods and return a more derived type.


jabbera wrote Apr 7, 2014 at 7:58 PM

Please Please Please!

AdamSpeight2008 wrote Apr 11, 2014 at 7:51 PM

You can already. Just wish I could remember where I put the example.
Idea you have a base (b) this is inherited by another base (f) that is genericaly constraint on b
 f<b> <: b
Now when you implement the concrete types (c) implement f<c> . This way you push the complex operators (eg addition, subtract) down into generic f . The the concrete types can then exploit them.

The example of mine implemented a basic units of measure.

AdamSpeight2008 wrote Apr 16, 2014 at 2:39 AM

jabbera, I've found the project I mentioned above. (A Unit System

I think the convert function in the iClass UnitOfDistance may be interesting to you.

MgSam wrote May 22, 2014 at 6:56 PM

I know this feature is not supported by the CLR directly, but perhaps C# could do the same sort of method re-writing C++ CLI does?