Friday, February 4, 2011

What languages support covariance on inherited methods' return types?

I discovered that the cause of my problems (see question) was the lack of support in C# for covariance on inherited methods' return types. Now I'm curious what languages support this feature. Accepted answer will be whoever can name the most.

EDIT: John Millikin correctly pointed out that lots of dynamic languages support this. I'll have to clarify that I'm looking for static/strongly typed languages.

  • Any dynamic languages, of course -- Python, Ruby, Smalltalk, Javascript, etc.

  • @John Millikin I see that you removed Java from the list because "Java only allows it for objects in the same inheritance hierarchy" but I think thats what I'm asking for..or is it?

    From Luke
  • 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.

  • Basically what I'm asking is what languages support what I'm trying to do here.

    From Luke
  • 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().

  • C++ supports covariant return types.

    MSN

  • Yeah originally when I asked that it was methods..I don't think its really possible with properties since covariance on the parameter type would not work. I'm going to update that question to be functions again. Sorry if I sucked you into an unanswerable question!

    From Luke
    • C++
    • Java
    • REALbasic
    • Eiffel
    • Sather
    • Modula-3
  • 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.

    From mlbrock

0 comments:

Post a Comment