score:3
This is occurring because, in class C, while you know U is a subtype of B, and T is a subtype of A, you are not guaranteeing that U is a subtype of T.
EDIT: I think I've found a way to do this. It uses generalised type constraints:
abstract class C[T <: A, U <: B] {
def addTwo(t: T): Int = t.a + 2
def fail(u: U)(implicit ev: U <:< T): Int = addTwo(u) // compiles
}
I haven't done a full test of this, though.
score:1
I don't know what C is modelling, but I would have said:
scala> class C[T <: A, U <: B] { def f(t: T) = t.a ; def g[X <: U with T](x: X) = f(x) }
defined class C
That is, you're just trying to say that whatever T and U are, the thing you pass to g is both.
Like the other answer says, where you put the constraints depends on what you're trying to do.
Source: stackoverflow.com
Related Query
- Why doesn't Scala infer type parameters the same way in my abstract class?
- Why can't scala infer the type of the omitted parameters in partial application?
- why scala can't infer the type of method parameters
- Why does Scala not infer the type parameters when pattern matching with @
- Why doesn't Scala fully infer type parameters when type parameters are nested?
- Why scala can't infer the type in a partial method?
- Why can't Scala infer the type of _?
- Why can't Scala infer the type parameter in this example?
- Scala Puzzle: enforcing that two function arguments are of the same type AND both are a subtype of a given class
- Abstract and Final class defined on the same Scala Class
- Scala 2.10 reflection: Why do I get the same type "List()" for the list and list element?
- Question on the initialization of abstract type class in scala
- Why scala compiler doesn't infer type with abstract type
- In Scala all abstract types have the same type after erasure
- Why doesn't scala infer the type members of an inherited trait?
- Accounting for type parameters in a Scala generic class 'equals' method... are manifests the only way?
- Why doesn't Scala's implicit class work when one of the type parameters should be Nothing?
- Why Scala Infer the Bottom Type when the type parameter is not specified?
- Why the following Scala code does not compile unless explicit type parameters are added?
- Is there a way to expand the scope of an existential type quantifier in Scala to convince the type checker that two variables have the same type?
- Why is Scala unable to infer the type of an anonymous function when there is method overloading and only one method takes a function?
- In Scala, why does a type annotation must follow for the function parameters ? Why does the compiler not infer the function parameter types?
- Instantiate class with type parameters with the type of a variable in Scala
- Why doesn't Scala infer the right type of ValidationNel?
- How to get the type parameters from an abstract class that is extended by an object
- why does a scala class in the worksheet with same name but different case as the worksheet cause an exception?
- Why is the best way to avoid being trapped by Scala's abstract type refinement?
- Why is Scala losing the actual type when it is assigned to an abstract type?
- Scala how to pass in child class and then return the same child class type while getting it generic?
- Why is scala failing to infer the type of mapped NodeSeq?
More Query from same tag
- Clojure pattern matching on argument's class
- Change gremlin scala return type List[Nothing] to List[String]
- Changing appends programmatically
- Accessing session information inside a custom PartHandler in Play! Framework 2.0
- Akka HTTP: How to split routes with actors
- After unsubscribe my subscription keeps running
- How to calculate hour to day in NetCDf file using scala
- Specify Java version in Java - scala mixed project in Intellij
- Scala: Contains in mutable and immutable sets
- How to add a special-case row in a empty Spark DataFrame?
- Scala 2 futures containing api calls race condition
- error: could not find implicit value for parameter d: io.circe.Decoder[List[TableInfo]]
- Akka StreamTcp timeout restart
- Scala's Implementation of haskell last method
- Why is it safe not to check object-private or object-protected definitions for their variance position?
- SQLContext implicits
- How to create a business logic for regular expression and save data to csv file
- dynamically parse json in flink map
- Generic Point Addition in Scala
- Scala string pattern matching for html tagged contents extraction
- Akka streams: Reading multiple files
- Scala case class with function parameters
- Split RDD[String] to RDD[tuples]
- Why the Scala method isInstanceOf[T] is not working
- default case class argument depending on other arguments scala
- akka custom fork-join-executor dispatcher behaves differently on OSX and RHEL
- Try to pass a function to a value but the function run
- Compile Spark stream kinesis sample application using SBT tool
- Stateful implementation of F-algebra
- How to update/refresh a parameter in Flink application