trait A[-T] { def m(x: T): Any = "A" }
trait B extends A[String] { override def m(x: String): Any = "B" }
val o = new A[Any] with B
assert(o.m("") == (o: A[String]).m(""))
scala> trait A[-T] { def m(x: T): Any }
defined trait A
scala> trait B extends A[String] { override def m(x: String): Any = "B" }
defined trait B
scala> val o = new A[Any] with B
<console>:38: error: object creation impossible, since method m in trait A of type (x: Any)Any is not defined
(Note that T does not match String)
val o = new A[Any] with B
^
There's something I'm probably missing there, but this looks like it would require multiple dispatch to workThe plain old visitor does what's needed though:trait AnimalVisitor[T] {
def visitDefault(animal: Animal): T
def visit(animal: Animal): T = visitDefault(animal)
def visit(animal: Cat): T = visitDefault(animal)
}
class Animal {
def accept[T](visitor: AnimalVisitor[T]): T = visitor.visit(this)
}
class Cat extends Animal {
override def accept[T](visitor: AnimalVisitor[T]): T = visitor.visit(this)
}
trait TraitA[-A <: Animal] {
def xyzVisitor() = new AnimalVisitor[Unit] {
def visitDefault(animal: Animal) = println("TraitA.xyz(...: A)")
}
def xyz(args: A): Unit = args.accept(xyzVisitor())
}
trait TraitB extends TraitA[Cat] {override def xyzVisitor() = new AnimalVisitor[Unit] {
def visitDefault(animal: Animal) = TraitB.super.xyzVisitor().visitDefault(animal)
override def visit(animal: Cat) = println("TraitB.xyz(...: Cat)")
}
}
class ClassB extends TraitB with TraitA[Animal]
object Test1 extends App {
(new ClassB()).xyz(new Cat())(new ClassB(): TraitA[Cat]).xyz(new Cat {})
}Then again, maybe I'm missing somethingThanks,David--On 30 September 2016 at 19:56, David Miguel Antunes <davidbr...@gmail.com> wrote:Yep, that's what I also thought...Here's a reduced version of this behavior:class Animal
class Cat extends Animal
trait TraitA[-A] {
def xyz(args: A): Unit = println("TraitA.xyz(...: A)")
}
trait TraitB extends TraitA[Cat] {
override def xyz(args: Cat): Unit = println("TraitB.xyz(...: Cat)")
}
class ClassB extends TraitB with TraitA[Animal]
object Test1 extends App {
(new ClassB()).xyz(new Cat())
(new ClassB()).xyz(new Cat(): Animal)
(new ClassB(): TraitA[Cat]).xyz(new Cat {})
}[info] Running Test1
TraitB.xyz(...: Cat)
TraitA.xyz(...: A)
TraitA.xyz(...: A)Thanks,DavidOn 30 September 2016 at 19:20, Oliver Ruebenacker <cur...@gmail.com> wrote:Best, OliverI would expect that in case of overriding of a method, the runtime type of an object should decide which implementation of the method is called, so this clearly seems like a compiler bug to me.Hello,On Fri, Sep 30, 2016 at 11:03 AM, David Miguel Antunes <davidbr...@gmail.com> wrote:--Hi,When I run this code:trait Animal
trait Cat extends Animal
trait TraitA[-A] { def xyz()(implicit args: A): Unit = println("TraitA")}
trait TraitB extends TraitA[Cat] {override def xyz()(implicit args: Cat): Unit = println("TraitB")}
class ClassA extends TraitA[Animal]
class ClassB extends ClassA with TraitB
object Test1 extends App {
val ref1: ClassB = new ClassB()
val ref2: TraitA[Cat] = ref1
ref1.xyz()(new Cat {})
ref2.xyz()(new Cat {})
}I get this output:[info] Running Test1TraitBTraitAIs this the expected behavior? or is this a bug? - if so is there a workaround?Thanks!David
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
class Animal
class Cat extends Animal
trait Writer[-T] {
def write(args: T) = println("Writer.write(...: T)")
}
trait AnimalWriter extends Writer[Animal] {
override def write(args: Animal) = println("AnimalWriter.write(...: Animal)")
}
trait CatWriter extends Writer[Cat] {
override def write(args: Cat) = println("CatWriter.write(...: Cat)")
}
class MyWriter extends AnimalWriter with CatWriter
object Test1 extends App {
(new MyWriter(): MyWriter).write(new Cat()) // Produces: "CatWriter.write(...: Cat)"
(new MyWriter(): CatWriter).write(new Cat()) // Produces: "CatWriter.write(...: Cat)"
(new MyWriter(): AnimalWriter).write(new Cat()) // Produces: "AnimalWriter.write(...: Animal)"
}
Thanks,David
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+unsubscribe@googlegroups.com.
Thanks,David
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
--
You received this message because you are subscribed to the Google Groups "scala-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-user+...@googlegroups.com.
So the move away from JIRA has begun?
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.
You received this message because you are subscribed to the Google Groups "scala-internals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-interna...@googlegroups.com.