开发者

How do languages Scala which need covariant return types and "real" class variance run on the CLR?

The CLR does not support covariant return types or full variance (i. e. applied to classes, not only interfaces and delegates), but there are langu开发者_StackOverflow社区ages targeting the CLR which use one or both of these features.

Is there some practical workaround for the CLR to enable this functionality or do these languages employ some kind of rewriting/erasure/... technique to fully support their feature set?


Probably the same way that Java does it (Java 5 supports covariant returns at the language level, but the JVM doesn't support it): by adding synthetic methods. Here's how Java does it: say you have a class like this:

class Foo implements Cloneable {
    @Override
    public Foo clone() {
        // ...
    }
}

Behind the scenes, two clone methods get generated: public Foo clone() (which contains the real code), and public Object clone() (which simply returns the result of the former). The latter method (which is synthesised) is how the clone method gets to be overridden at the JVM level.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜