score:3
Accepted answer
Good question. I don't think you can do what you want directly.
One alternative approach is trait IntStrDependent extends Dependent[Either[Int, String]]
but that doesn't quite solve the problem. Maybe a variant of Miles Sabin's encoding of union types allows something fancier to be done.
I think the best option is to keep it simple,
trait Dependent[T]{
def observeCritereaChanged(oldValue:T, newValue:T):Unit
}
trait IntStrDependent {
val I: Dependent[Int]
val S: Dependent[String]
}
object MyDependent extends IntStrDependent {
object I extends Dependent[Int] {
def observeCritereaChanged(oldValue:Int, newValue:Int) {}
}
object S extends Dependent[String] {
def observeCritereaChanged(oldValue:String, newValue:String) {}
}
}
To use MyDependent
, one must explicitly select the Int
or String
variant, as in
MyDependent.I.observeCritereaChanged(1, 2)
In my opinion, making the type dependence explicit is a good thing anyway.
Source: stackoverflow.com
Related Query
- Composition with the same trait, but different type parameters
- How to use shapeless HList to have a list of the same type but with a different generic?
- two type parameters with the same name
- Different performance of object with same runtime class but different static type
- How can a method accept a (T <: Seq)[A] and return T[B] -- the same type of sequence with a different type parameter?
- Is there any method that does the same thing as map() but generates a different type of container?
- scala: union of two maps whose key type is the same and whose value type is a collection of elements, but whose types are different
- Providing multiple instances of same implicit specialized with different type parameters
- How to mix in traits with implicit vals of the same name but different types?
- why does a scala class in the worksheet with same name but different case as the worksheet cause an exception?
- Combine two Maps with same key type, but different value type in scala
- Is it a library bug in a functional language when a function with the same name but for different collections produces different side effects?
- Define a common trait for types with different numbers of type parameters
- List of the same type as my trait, but only with the implementations
- Can Scala method that returns a trait returns method with the same type as trait?
- Implement a final def within some trait where type parameters may or may not be the same
- Implicit parameters with 2 instances of the same type
- What is the best design for set of elements with different type parameters in Scala
- In scala multiple inheritance, how to resolve conflicting methods with same signature but different return type?
- Why is Scala's behavior in case of overloading with by-name parameters different from the case with by-value parameters?
- Multiple constructors with the same number of parameters exception while transforming data in spark using scala
- How can I improve Scala's type inference with type parameters that don't show up in the first parameter list?
- Returning the same type from abstract trait method
- Trait self type bound: A with B but not A with C
- Scala parameterized type problem with returning an instance of the same type
- (In Scala,) Is there anything that can be done with generic type parameters of classes but not with abstract type members?
- When create two different Spark Pair RDD with same key set, will Spark distribute partition with same key to the same machine?
- 2 Extension Methods with the same name in different classes do not work in Scala 3?
- Why with scala, using the same regular expression, using 2 different matching methods lead to 2 different results?
- Convert one case class to another with the same structure but with one additional field
More Query from same tag
- Strange behaviour in curly braces vs braces in scala
- Treating a constructor as a function in Scala - how to put constructors in a map?
- How to work around DataSet.toJSON being incompatible with Structured Streaming
- Helper method with accumulators in Scala
- Akka scala Event bus with different classifiers depending on the subscriber
- Using object constructor of a trait
- Method with a dynamic number of type parameter
- Why Scala return in for-loop give Unit value type?
- Passing parameters to scalameta paradise macro
- How can I apply the same Pattern on two different Kafka Streams in Flink?
- Scala 3 implicit conversions: compare value and literal
- What was the reason to restrict on combining implicit parameters and view/context bounds?
- Create custom DynamoDB format for my sealed Trait
- Programmatically Rename All But One Column Spark Scala
- Are there tools for code obfuscation for Scala?
- reading json file using jackson library in scala language
- scala spark dataframe explode is slow - so, alternate method - create columns and rows from arrays in a column
- Passing elements of a List as parameters to a function with variable arguments
- Spark Scala Generating Random RDD with (1's and 0's )?
- Scala Override Return Type
- SBT-web incremental compilation with file dependencies
- Issue with generic type bounds
- Possible to make scala require a non-Nothing generic method parameter and return to type-safety
- Disposable Resource Pattern
- Why can't I omit the implicit parameter here?
- Apply several string transformations in scala
- akka firstCompletedOf, identify message sender
- How to write custom scala slick column mapping
- scala compiler complains weirdly when compiling a foldleft with scalaz function
- Cats Effect: which thread pool to use for Non-Blocking IO?