What languages support covariance on inherited methods' return types?
-
09-06-2019 - |
Question
I originally asked this question, but in finding an answer, discovered that my original problem was a lack of support in C# for covariance on inherited methods' return types. After discovering that, I became curious as to what languages do support this feature.
I will accept the answer of whoever can name the most.
EDIT: John Millikin correctly pointed out that lots of dynamic languages support this. To clarify:
I am only looking for static/strongly typed languages.
Solution
- C++
- Java
- REALbasic
- Eiffel
- Sather
- Modula-3
OTHER TIPS
Any dynamic languages, of course -- Python, Ruby, Smalltalk, Javascript, etc.
Basically what I'm asking is what languages support what I'm trying to do here.
Does C# let you specify different data types for the get()
and set()
methods? If not, I would split them into actual Leg get_leg()
and set_leg(DogLeg)
functions. Otherwise one of two things will happen: 1) overspecification of get_leg()
2) underspecification of set_leg()
.
Java added support for this in 1.5. It will not compile in earlier versions.
As pointed out by Ivan Hamilton and Mat Noguchi, C++ supports the feature. But note that covariant return types are broken for template classes which inherit from some base in MSVC 7.X through 9.X (and probably 6 also). You get error C2555.
C++ supports covariant return types.
but I think thats what I'm asking for..or is it?
I frankly don't know what you're asking. Java apparently has the same support for return-type covariance as C#, so if whatever you're looking for is lacking in C#, it's lacking in Java also.