Generics : List

List<Dog> is a subtype of List<? extends Animal>, but not a subtype of List<Animal>. Why is List<Dog> not a subtype of List<Animal>? Consider the following example: void mySub(List<Animal> myList) { myList.add(new Cat()); } If you were allowed to pass a List<Dog> to this function, you would get a run-time error. EDIT: Now, if we use … Read more

How do I return a reference to something inside a RefCell without breaking encapsulation?

Instead of creating a brand new type, you can use Ref::map (since Rust 1.8). This has the same result as Levans’ existing answer: use std::cell::Ref; impl Foo { pub fn get_items(&self) -> Ref<‘_, Vec<i32>> { Ref::map(self.interior.borrow(), |mi| &mi.vec) } } You can also use new features like impl Trait to hide the Ref from the … Read more

Why aren’t there many discussions about co- and contra-variance in Haskell (as opposed to Scala or C#)?

There are two main reasons: Haskell lacks an inherent notion of subtyping, so in general variance is less relevant. Contravariance mostly appears where mutability is involved, so most data types in Haskell would simply be covariant and there’d be little value to distinguishing that explicitly. However, the concepts do apply–for instance, the lifting operation performed … Read more

Give examples of functions which demonstrate covariance and contravariance in the cases of both overloading and overriding in Java? [closed]

Covariance: class Super { Object getSomething(){} } class Sub extends Super { String getSomething() {} } Sub#getSomething is covariant because it returns a subclass of the return type of Super#getSomething (but fullfills the contract of Super.getSomething()) Contravariance class Super{ void doSomething(String parameter) } class Sub extends Super{ void doSomething(Object parameter) } Sub#doSomething is contravariant because … Read more

Why doesn’t the example compile, aka how does (co-, contra-, and in-) variance work?

Generically, a covariant type parameter is one which is allowed to vary down as the class is subtyped (alternatively, vary with subtyping, hence the “co-” prefix). More concretely: trait List[+A] List[Int] is a subtype of List[AnyVal] because Int is a subtype of AnyVal. This means that you may provide an instance of List[Int] when a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)