covariant

Scala anonymous function genric variance issues

江枫思渺然 提交于 2019-12-13 05:15:01
问题 I'm on the road to learn Scala and I'm having a hard time understanding contravariants, covariants, invariance, etc. From Why doesn't the example compile, aka how does (co-, contra-, and in-) variance work? I have learned how functions can be considered subtypes of another function. (Really useful to know!) The code below is what I believe are the important pieces to solving my puzzle. I have extracted parts that I think would add unneeded complexity to the problem. According to the example I

“Invalid covariant return type” errors in nested classes with methods returning template-based objects

青春壹個敷衍的年華 提交于 2019-12-11 05:27:15
问题 The following C++ code gives me these errors when compiled: covariant.cpp:32:22: error: invalid covariant return type for ‘virtual Q<B> C::test()’ covariant.cpp:22:22: error: overriding ‘virtual Q<A> B::test()’ I do not want to change the line virtual Q<B> test() {} to virtual Q<A> test() {} although it removes the compilation errors. Is there another way to solve this problem? template <class T> class Q { public: Q() {} virtual ~Q() {} }; class A { public: A() {} virtual ~A() {} }; class B {

Covariant return type with non-pointer/reference return type

眉间皱痕 提交于 2019-12-10 23:18:07
问题 I'm trying to implement a .NET framework like collection class in C++(11). My problem is an invalid covariant type. I have these classes: template<typename T> class IEnumerator { public: virtual bool MoveNext() = 0; //... }; template<typename T> class IEnumerable { virtual IEnumerator<T> GetEnumerator() = 0; }; template<typename T> class List : public IEnumerable<T> { public: struct Enumerator : public IEnumerator<T> { Enumerator(List<T> &list) { //... } // ... }; Enumerator GetEnumerator() {

java covariant return type

孤街醉人 提交于 2019-12-10 12:55:19
问题 Why does below code prints "1" ? class A { int x = 1; } class B extends A { int x = 2; } class Base { A getObject() { System.out.println("Base"); return new B(); } } public class CovariantReturn extends Base { B getObject() { System.out.println("CovariantReturn"); return new B(); } /** * @param args */ public static void main(String[] args) { Base test = new CovariantReturn(); System.out.println(test.getObject() instanceof B); System.out.println(test.getObject().x); } } 回答1: Because you are

Covariant Return Type in Interface not compiling via Javac

不羁的心 提交于 2019-12-10 02:52:07
问题 I have the following structure: public interface BarReturn {} public interface FooReturn {} public interface FooBarReturn extends FooReturn, BarReturn {} public interface Foo { FooReturn fooBar( ); } public interface Bar { BarReturn fooBar(); } public interface FooBar extends Foo, Bar { FooBarReturn fooBar(); } Javac fails with the following message: FooBar.java:2: types Bar and Foo are incompatible; both define fooBar(), but with unrelated return types public interface FooBar extends Foo,

Covariant Return Type in Interface not compiling via Javac

白昼怎懂夜的黑 提交于 2019-12-05 02:52:45
I have the following structure: public interface BarReturn {} public interface FooReturn {} public interface FooBarReturn extends FooReturn, BarReturn {} public interface Foo { FooReturn fooBar( ); } public interface Bar { BarReturn fooBar(); } public interface FooBar extends Foo, Bar { FooBarReturn fooBar(); } Javac fails with the following message: FooBar.java:2: types Bar and Foo are incompatible; both define fooBar(), but with unrelated return types public interface FooBar extends Foo, Bar { ^ 1 error However, Eclipse can compile it fine, and as far as I can see it should compile - FooBar

Why doesn't Java 5+ API take advantage of covariant return types?

為{幸葍}努か 提交于 2019-11-30 14:58:38
问题 Since Java 5 we are allowed to have covariant return types. Why doesn't the Java API take advantage of this? Take Graphics2D.create() for instance. Why isn't it overridden to return a Graphics2D object? It seems to me that it would be backward compatible in all situations. 回答1: In general, this is indeed in order to maintain backward compatibility. Note that the compatibility must be kept on the bytecode level too, and changing the return type changes the bytecode. So in general, if there are

Why doesn't Java 5+ API take advantage of covariant return types?

我是研究僧i 提交于 2019-11-30 13:02:53
Since Java 5 we are allowed to have covariant return types . Why doesn't the Java API take advantage of this? Take Graphics2D.create() for instance. Why isn't it overridden to return a Graphics2D object? It seems to me that it would be backward compatible in all situations. In general, this is indeed in order to maintain backward compatibility. Note that the compatibility must be kept on the bytecode level too, and changing the return type changes the bytecode. So in general, if there are any subclasses which may have overridden the method in question, switching to a covariant return type

c# covariant generic parameter

爷,独闯天下 提交于 2019-11-30 06:23:32
问题 I'm trying to understand this but I didn't get any appropriate results from searching. In c# 4, I can do public interface IFoo<out T> { } How is this different from public interface IFoo<T> { } All I know is the out makes the generic parameter covariant (??). Can someone explain the usage of <out T> part with an example? And also why is applicable only for interfaces and delegates and not for classes? Sorry if it's a duplicate and close it as such if it is. 回答1: Can someone explain the usage

“Invalid covariant return type” errors in nested classes with methods returning template-based objects

邮差的信 提交于 2019-11-29 10:53:50
The following C++ code gives me these errors when compiled: covariant.cpp:32:22: error: invalid covariant return type for ‘virtual Q<B> C::test()’ covariant.cpp:22:22: error: overriding ‘virtual Q<A> B::test()’ I do not want to change the line virtual Q<B> test() {} to virtual Q<A> test() {} although it removes the compilation errors. Is there another way to solve this problem? template <class T> class Q { public: Q() {} virtual ~Q() {} }; class A { public: A() {} virtual ~A() {} }; class B { public: B() {} virtual ~B() {} virtual Q<A> test() = 0; }; class C : public B { public: C() {} virtual