score:17
I'm afraid there is no way to do that. The super[A].work
way works only if A
and B
have the same return types.
Consider this:
class D extends B
....
val test: List[B] = List(new C(), new D())
test.map(b => b.work) //oops - C returns a String, D returns an Int
score:6
Scala simply prevents you from mixing A
and B
together if they declare a method with the same name and incompatible signature.
score:6
You can't do that.
Look at this piece of code:
val c = new C
val a: A = c
val b: B = c
There is no way that both of these lines could work:
val s: String = a.work
val i: Int = b.work
If we allowed such code to compile, one of these assignments would have to throw a ClassCastException
or fail in another way. So, it simply isn't possible to resolve such conflict.
I guess you have to workaround this with some form of delegation, maybe something like this:
class C extends A {
def toB = new B {
//implement B methods by somehow delegating them to C instance
}
}
score:6
You can't do that in Scala.
The way to work this around is to use the traits as collaborators
trait A {
def work = { "x" }
}
trait B {
def work = { 1 }
}
class C {
val a = new A { }
val b = new B { }
a.work
b.work
}
Source: stackoverflow.com
Related Query
- In scala multiple inheritance, how to resolve conflicting methods with same signature but different return type?
- How to implement Java interface in Scala with multiple variable parameter methods (type eraser issue)?
- Calling different methods from different traits in scala with multiple inheritance
- Scala - how to make the SortedSet with custom ordering hold multiple different objects that have the same value by which we sort?
- How do I disambiguate in Scala between methods with vararg and without
- How to have SBT subproject with multiple Scala versions?
- How to create a Scala class with private field with public getter, and primary constructor taking a parameter of the same name
- How do you create scala anonymous function with multiple implicit parameters
- Multiple constructors with the same number of parameters exception while transforming data in spark using scala
- Scala - Multiple inheritance with App trait
- 2 Extension Methods with the same name in different classes do not work in Scala 3?
- calling multiple functions with same instance in scala
- Scala declare multiple variables at the same row with the first character as upper-case
- How to implement maxBy with multiple max in Scala
- Is this a correct understanding of how Scala provides multiple inheritance without causing the Diamond issue
- How do you declare a function in Scala that returns itself (or another function with the same signature)?
- Scala class inheritance with same variable name
- How to solve the inheritance of conflicting members in Scala
- How can I predict which implementation will be chosen when mixing in multiple traits with conflicting abstract overrides?
- How to get match type with multiple type parameters to work correctly in Scala 3
- How can I resolve conflicting actor systems while testing akka-http and akka actors at the same spec file?
- Tail recursiveness in Scala methods with multiple parameter lists
- Scala Adapter pattern - autommagically allow "duck typing" for classes with same methods
- Perform multiple aggregations on different columns in same dataframe with alias Spark Scala
- In a scala project with multiple modules, where to define values accessible globally in all the build.sbt's within the same project
- How do I explode multiple columns of arrays in a Spark Scala dataframe when the columns contain arrays that line up with one another?
- How do parse fixed-position file with multiple sections in Spark using Scala
- How execution of same function defined with "_" is different from the same defined with a named variable in Scala
- How to map multiple Futures and pass them as arguments to a view using Play with Scala
- How to access members of container class with same name? scala
More Query from same tag
- play framework and requirejs development javascript code
- How to get rid of this boiler plate code
- Streaming data in and out simultaneously on a single HTTP connection in play
- Extracting substring using Scala regex
- How to specify the execution order of Specs2 tests?
- Is it possible to make an Akka HTTP core client request inside an Actor?
- How does a lazy val solve forward reference extends over definition of value in scala
- Percentile calculator
- Spark import of the mllib package member
- What is the proper way to return from an exception in Scala?
- in scala what is the A in sum[B >: A](implicit num: Numeric[B]): B
- Logstash scala log parsing
- JSON4S does not serialize internal case class members
- Scala's wrong forward reference error
- Spark SQL window function look ahead and complex function
- How to remove objects from unstructured json by key-value with play json
- Scala - anonymous object select query
- Scala case class generated field value
- Scala, @ResponseBody, and Map
- Intellij Idea Code Coverage Vs Maven Jacoco
- What is the meaning of "val Class.Something(2,_) = doSomething()"?
- Count the words for each country in a textfile via RDD
- object SparkHadoopUtil in package deploy cannot be accessed in package org.apache.spark.deploy
- Scala Array Copy looks to be working differently
- Adding artifact to play project using SBT - Native Packager Plugin
- Get Results From URL using Scala Spark
- Intellij Idea Kotlin plugin cannot see scala case class accessors
- Mark object as requiring some 'self-type' annotations
- Issue understanding splitting data in Scala using "randomSplit" for Machine Learning purpose
- Scala dispatch: Set default hostname verifier?