score:4
Accepted answer
Using shapeless you can write:
import shapeless._
class SpecificAnimalContainer[A <: Animal](a: A)(implicit ev: A =:!= Animal) {
def specificAnimal: A = a
}
// val animalContainer = new SpecificAnimalContainer[Animal](new Animal)// doesn't compile
Otherwise you can implement similar type for implicit yourself.
score:0
It's a bit unclear what you're trying to achieve, but your problem looks exactly like a book example from Scala documentation at https://docs.scala-lang.org/tour/upper-type-bounds.html
abstract class Pet extends Animal {}
class PetContainer[P <: Pet](p: P) {
def pet: P = p
}
class Lion extends Animal {
override def name: String = "Lion"
}
// val lionContainer = new PetContainer[Lion](new Lion)
// ^this would not compile
Hope this helps
Source: stackoverflow.com
Related Query
- In Scala how do I define upper type bounds that are exclusive of the defined class?
- Scala classOf for type parameter: how to call the function and how to restrict with upper type bounds
- How to get the properties values those that are defined in the shell (bash) script file using Scala 2.11 code?
- How are Scala collections able to return the correct collection type from a map operation?
- How to use the function type in scala within defined in type meaningfully?
- How to define a scala method with type param that cannot be Any
- Scala - how to define a structural type that refers to itself?
- Scala Puzzle: enforcing that two function arguments are of the same type AND both are a subtype of a given class
- scala.tools.nsc.Interpreter -- How to execute interpreter statements so that the results are defined in the global scope? (Scala 2.7.7final)
- How does the Scala type checker know to prevent calling flatten on a List that is already flat
- scala: how to create a generic type which is subtype of all the number classes in scala so that it can include compare method
- How does the Scala type system know that cons + Nil is exhaustive?
- How can I ensure that the dynamic type of my custom Scala collection is preserved during a map()?
- How to write efficient type bounded code if the types are unrelated in Scala
- How can I create a generic list monoid in scala that maintains the inner type of the list involved?
- How define Structural Type that the method return this
- how to define upper bounds for scala method
- Scala Reflection: How to find a no-arg constructor via reflection (if there are multiple constructors) ? What is the type signature of "no-arg"?
- How to model operations in Scala that are only applicable to the same concrete implementations of a trait?
- How to make it so that dependent types in two different traits are recognized as the same type
- What are the way/s of telling scala compiler that type TableQuery[T] can map to somthing has the attribute id
- How to pass type parameters to my scala application? Are factories the best solution?
- Scala Multiple type parameter that are defined later
- Scala how to let scala commit to the specified type bounds
- Spark scala how to remove the columns that are not in common between 2 dataframes
- How does the Scala program compile, given that bindAndHandle method takes the first parameter of type Flow not Route?
- How do I get all the keys that are stored in the Cassandra 2.0.1 column family with scala using hector?
- How to see the list of JARs that are loaded with my Scala Application
- In scala how do you enforce the encoder to operate on the type when gets inserted into a generic function that only enforces certain traits?
- How do I view the type of a scala expression in IntelliJ
More Query from same tag
- How to declare a sorted set of array of ints in Scala?
- What is the proper way of calling a superclass constructor in Scala?
- How to split dataset to two datasets with unique and duplicate rows each?
- Spark: RDD to List
- sbt-assembly not found when building Spark 0.5
- How to generate javadoc-style documentation for Scala-only project in sbt?
- Confused about a few lines code in a scala official document page
- Parse Numeric OR String value from Json response - Scala - Play
- Scala: How to use case keyword without match keyword?
- spark scala code value inside map
- What is a combinator in Scala or functional programming
- Scala how to instantiate new Option[java.sql.Date] type
- java.util.concurrent.TimeoutException: Request timed out to in gatling
- Read ORC files directly from Spark shell
- Scalaz Validation with applicative functor |@| not working
- In Scala, how would I take a Spark RDD, and output to different files, grouped by the values of a column?
- Import statement:error:not found in Scala REPL
- ScheduledThreadPoolExecutor scala
- My schema is nullable but still giving ArrayIndexOutOfBoundsException: 1
- sbt compile yields "object casbah is not a member of package com.mongodb"
- Existential types in Scala
- Referencing an abstract type from a parameterized type
- Class Type T - Explained
- get values from BsonArray in scala
- Can't understand why `trait Hello[+A] { def test[B<:A] }` can't be compiled
- FS2 through2 closing all resources when the first stream is finished?
- How can I delete all records from a table with squeryl?
- Should all/most methods in interfaces return Option?
- Spark atop of Docker not accepting jobs
- Apache Spark how to append new column from list/array to Spark dataframe