score:0
Accepted answer
If you want scalac to show an error at compile time when a wrong Feature
is supplied, AND you cannot alter anything other than Service1
then it is not possible. The compiler accepts or rejects a call to Service1.containsFeature
based on its interface, but its interface is already defined in Service
where it says that it will accept any Feature
.
If you CAN change things about the other code, there are some ways to do this. For instance if you can change everything:
scala> :paste
// Entering paste mode (ctrl-D to finish)
abstract class Service[Allowed <: Feature] {
def containsFeatures(x: Allowed*): Unit
}
object Service1 extends Service[S1Feature] {
def containsFeatures(x: S1Feature*): Unit = println("ok")
}
object Service2 extends Service[S2Feature] {
def containsFeatures(x: S2Feature*): Unit = println("ok")
}
sealed trait Feature
sealed trait S1Feature extends Feature
sealed trait S2Feature extends Feature
case object A extends S1Feature
case object B extends S2Feature
case object C extends S1Feature
case object D extends S2Feature
case object E extends S1Feature
case object F extends S2Feature
case object G extends S1Feature
// Exiting paste mode, now interpreting.
scala> Service1.containsFeatures(A,B,C)
<console>:16: error: type mismatch;
found : B.type
required: S1Feature
Service1.containsFeatures(A,B,C)
^
scala> Service1.containsFeatures(A,C,G)
ok
Source: stackoverflow.com
Related Query
- Composing subclasses to achieve type restriction
- Composing Option with List in for-comprehension gives type mismatch depending on order
- Is there example of scala abstract type usage which is impossible to achieve with generics?
- How to explain TreeNode type restriction and self-type in Spark's TreeNode?
- Scala lower type bound 'is a subtype' restriction on method arguments
- Good ways to create a Scala class whose Type parameter depends on composing object Type
- What is the correct restriction on a type projection in Scala?
- Scala type signature failing with subclasses
- Return a Java collection of type and subclasses from Scala
- How to force subclasses to override an inherited method with a more specific return type
- How to achieve argument and path dependent type parametrization in constructor?
- Enforce that all subclasses implement a given type class
- Scala Type Mismatch For Subclasses of The Same Parent
- Scala type mismatch when composing traits
- Scala generic type function restriction like F#
- Subtype restriction on type parameter
- Find direct known subclasses with type parameter in macro
- How do I get around type erasure on Scala? Or, why can't I get the type parameter of my collections?
- What is a higher kinded type in Scala?
- Getting a structural type with an anonymous class's methods from a macro
- What are type lambdas in Scala and what are their benefits?
- Limits of Nat type in Shapeless
- How do I view the type of a scala expression in IntelliJ
- I want to get the type of a variable at runtime
- Scala type programming resources
- How does type Dynamic work and how to use it?
- Implicit conversion vs. type class
- Scala multiple type pattern matching
- Editor does not contain a main type
- Scala underscore - ERROR: missing parameter type for expanded function
More Query from same tag
- Scala3 extensions for basic types and overriding ==
- Scala: how to specify varargs as type?
- Spark Streaming + Hive
- Streaming K-means Spark Scala: Getting java.lang.NumberFormatException for input string
- Is there a way to send values from actor to actorRef in ActorSystem
- Scala - How to Return this Kind of RDD type
- Interpreting a benchmark in C, Clojure, Python, Ruby, Scala and others
- Getting first n distinct Key Tuples in Scala Spark
- Process item in a window with Kafka streams
- MySql - MySql server thinks there is no password when its provided in the URL
- Working with cookie in scalaj-http
- How do I attach scala sources to my maven jar?
- How can I apply the same Pattern on two different Kafka Streams in Flink?
- class not found error - scala
- Regex for LaTeX umlaut escapes?
- Convert \\ to \ in Scala
- Booyer-Moore or similar in Java or Scala Libraries
- SBT: SPARK2.0.*: SCALA: unresolved dependencies org.glassfish.hk2 and javax.validation
- bindFromRequest and asFormUrlEncoded return different values
- Reactive way of resizing images from MongoDB
- Retrieving neo4j nodes related to a given node using scala
- How to easily convert IndexedSeq[Array[Int]] to Seq[Seq[Int]] in Scala?
- Scala class substitution
- Issues when using Doobie library with Oracle and Timestamp
- Shapeless, how to prove Nat is within Sized boundaries?
- scala: toString for synthetic classes and functions
- Spark local testing reports OutOfMemoryError, how to fix?
- scala chained conditional mapping - `ifDefined` method
- Setting up sbt environment to hack on multiple libraries at once
- Resolving Akka futures from ask in the event of a failure