score:3
Maybe you want the return type to be wider than this.type
but narrower than just MaxPQ[U]
. Then try to introduce a type member
sealed trait MaxPQ[+U] {
type This <: MaxPQ[U]
// type This >: this.type <: MaxPQ[U] { type This = MaxPQ.this.This }
def insert[K >: U : Ordering](v: K): This
}
abstract class AbstractMaxPQ[U : Ordering] extends MaxPQ[U] {
override type This <: AbstractMaxPQ[U]
// override type This >: this.type <: AbstractMaxPQ[U] { type This = AbstractMaxPQ.this.This }
}
class ArrayMaxPQ[U : Ordering : ClassTag] extends AbstractMaxPQ[U] {
override type This = ArrayMaxPQ[U]
override def insert[K >: U : Ordering](v: K): ArrayMaxPQ[U] = ???
}
Returning the "Current" Type in Scala
If you want the return type to be parametrized with an upper bound of U
try to make This
higher-kinded
sealed trait MaxPQ[+U] {
type This[K >: U] <: MaxPQ[K]
// type This[K >: U] <: MaxPQ[K] { type This[K1 >: K] = MaxPQ.this.This[K1] }
def insert[K >: U : Ordering](v: K): This[K]
}
abstract class AbstractMaxPQ[U : Ordering] extends MaxPQ[U] {
override type This[K >: U] <: AbstractMaxPQ[K]
// override type This[K >: U] <: AbstractMaxPQ[K] { type This[K1 >: K] = AbstractMaxPQ.this.This[K1] }
}
class ArrayMaxPQ[U : Ordering : ClassTag] extends AbstractMaxPQ[U] {
override type This[K >: U] = ArrayMaxPQ[K]
override def insert[K >: U : Ordering](v: K): ArrayMaxPQ[K] = ???
}
score:3
You can actually use higher-kinded types to do this. It's called F-bounded polymorphism.
sealed trait MaxPQ[+U, F[_]] { self: F[_ <: U] =>
def insert[K >: U: Ordering](v: K): F[K]
}
abstract class AbstractMaxPQ[U: Ordering, F[_]] extends MaxPQ[U, F] {
self: F[U] =>
}
class ArrayMaxPQ[U: Ordering: ClassTag] extends AbstractMaxPQ[U, ArrayMaxPQ] {
override def insert[K >: U: Ordering](v: K): ArrayMaxPQ[K] = ???
}
Instead of passing G
into insert
, use it as a type parameter of the class/trait itself, and then make sure that this
extends that. That's what the self-type is for (self: F[U]
). Another useful question
Source: stackoverflow.com
Related Query
- Inherited return type with contravariant type parameter
- Name of Bi - Functor type class with one contravariant and one covariant parameter
- Pass a function with any case class return type as parameter
- Optional function parameter with generic return type
- Why scala cannot infer common return type for a function with multiple parameter lists?
- Scala filter by type parameter and return construct return type with type constructor
- How to force subclasses to override an inherited method with a more specific return type
- difficulty when using contravariant type parameters for both a type parameter and return type of a method
- Type mismatch found when implementing traits with parameter in the return
- Scala: implementing method with return type of concrete instance
- Minimal framework in Scala for collections with inheriting return type
- Scala doobie fragment with generic type parameter
- Scala: Declaring method with generic type parameter
- Scala: Implicit evidence for class with type parameter
- Cannot specialize a Scala method with specializable trait as return type
- Cannot infer contravariant Nothing type parameter
- Scala by Example - trait type parameter with context bounds mistake?
- why the first type parameter is defined as contravariant in Function1[-A, +B]?
- Scala: how to work with long type parameter lists
- Generate return type signature in Scala with Intellij Idea
- How to turn off Scala auto-completion of function with Unit return type in IntelliJ IDEA?
- Inferring type of generic implicit parameter from return type
- How can I improve Scala's type inference with type parameters that don't show up in the first parameter list?
- Scala path dependent return type from parameter
- Return most specific type given a method parameter
- Scala override method with subclass as parameter type
- Map on HList in method with Poly1 based on type parameter of class
- Type erasure with parameter defaults
- Trait does not conform with type parameter bounds
- How to create a custom Seq with bounded type parameter in Scala?
More Query from same tag
- Access token exception instagram with working access token?
- Setting invalid data to missing data in Spark DataFrames
- How to filter a List with another List based on some conditions?
- Scala Implicit convertions: 2 way to invoke
- Not able to fetch exact phrase from the Twitter API in Spark Streaming application
- scala sbt-launch.jar - multiple projects ihe same directory?
- Make ScalaCheck tests deterministic
- Logging in spark structured streaming / SparkException: Task not serializable
- Slick confused about numThreads and best practice for good performance
- behaviours of Val in Scala
- How to do some complex calculation between columns in spark Dataframe?
- Scala: Specifying public method overriding protected method
- Scala XML equality issues
- Connect Akka Streams to JMS
- Why it is not possible to override mutable variable in scala?
- What's the deal with all the Either cruft?
- Apache Tika 1.7: Parse files in a zip archive
- Higher order functions in scala
- Sockets and Streaming in Play Framework
- slick 3.2 - how to define an "abstract" trait with tables and queries definitions but no concrete profile?
- how to convert a list of string to case class?
- Scala: sliding(N,N) vs grouped(N)
- AutoRollback doesn't rollback
- How to convert a dataframe or RDD to a List of Tuples?
- How to handle recursion with monix's observable?
- Scala Mixin with self=> traits
- sbt.Resolve Exception : unresolved dependency: com.typesafe.sbteclipse#sbteclipse-plugin 2.4.0 not found
- Scala anonymous function syntax
- Scala DoubleLinkedList replacement
- Asynchronous coding in Scala