invariance

Scala - covariant type in mutable collections

六眼飞鱼酱① 提交于 2020-01-04 07:37:07
问题 I am new in Scala world and now I am reading the book called "Scala in Action" (by Nilanjan Raychaudhuri), namely the part called "Mutable object need to be invariant" on page 97 and I don't understand the following part which is taken directly from the mentioned book. Assume ListBuffer is covariant and the following code snippet works without any compilation problem: scala> val mxs: ListBuffer[String] = ListBuffer("pants") mxs: scala.collection.mutable.ListBuffer[String] = ListBuffer(pants)

How do I deal with wrapper type invariance in Rust?

我的未来我决定 提交于 2019-12-12 10:39:34
问题 References to wrapper types like &Rc<T> and &Box<T> are invariant in T ( &Rc<T> is not a &Rc<U> even if T is a U ). A concrete example of the issue (Rust Playground): use std::rc::Rc; use std::rc::Weak; trait MyTrait {} struct MyStruct { } impl MyTrait for MyStruct {} fn foo(rc_trait: Weak<MyTrait>) {} fn main() { let a = Rc::new(MyStruct {}); foo(Rc::downgrade(&a)); } This code results in the following error: <anon>:15:23: 15:25 error: mismatched types: expected `&alloc::rc::Rc<MyTrait>`,

Why can find `Functor` instance for Tree but not for Branch or Leaf?

て烟熏妆下的殇ゞ 提交于 2019-12-11 07:11:36
问题 I have the following Functor definition: import cats.Functor import cats.syntax.functor._ object Theory { implicit val treeFunctor: Functor[Tree] = new Functor[Tree] { def map[A, B](fa: Tree[A])(f: A => B): Tree[B] = fa match { case Branch(left, right) => Branch(map(left)(f), map(right)(f)) case Leaf(value) => Leaf(f(value)) } } def main(args: Array[String]): Unit = { Branch(Leaf(10), Leaf(20)).map(_ * 2) } } for: sealed trait Tree[+A] final case class Branch[A](left: Tree[A], right: Tree[A])

What are good reasons for choosing invariance in an API like Stream.reduce()?

本秂侑毒 提交于 2019-11-29 16:35:25
问题 Reviewing Java 8 Stream API design, I was surprised by the generic invariance on the Stream.reduce() arguments: <U> U reduce(U identity, BiFunction<U,? super T,U> accumulator, BinaryOperator<U> combiner) A seemingly more versatile version of the same API might have applied covariance / contravariance on individual references to U , such as: <U> U reduce(U identity, BiFunction<? super U, ? super T, ? extends U> accumulator, BiFunction<? super U, ? super U, ? extends U> combiner) This would

Vector{AbstractString} function parameter won't accept Vector{String} input in julia

夙愿已清 提交于 2019-11-28 01:02:27
The following code in Julia: function foo(a::Vector{AbstractString}) end foo(["a"]) gives the following error: ERROR: MethodError: no method matching foo(::Array{String,1}) Closest candidates are: foo(::Array{AbstractString,1}) at REPL[77]:2 Even though the following code runs, as expected: function foo(a::Vector{String}) end foo(["a"]) And further, AbstractString generally matches String as in: function foo(::AbstractString) end foo("a") How can I call a function with a Vector{AbstractString} parameter if I have String elements? StefanKarpinski You need to write the function signature like

Vector{AbstractString} function parameter won't accept Vector{String} input in julia

北战南征 提交于 2019-11-26 21:49:14
问题 The following code in Julia: function foo(a::Vector{AbstractString}) end foo(["a"]) gives the following error: ERROR: MethodError: no method matching foo(::Array{String,1}) Closest candidates are: foo(::Array{AbstractString,1}) at REPL[77]:2 Even though the following code runs, as expected: function foo(a::Vector{String}) end foo(["a"]) And further, AbstractString generally matches String as in: function foo(::AbstractString) end foo("a") How can I call a function with a Vector{AbstractString