Why Scala compiler cannot compile next code :
trait Profile {}
class SomeProfile extends Profile
trait Foo {
def get[T <: Profile]: Option[T]
}
object Example {
val foo: Foo = new Foo {
// This works (but might give runtime exception), but it is not ugly? :)
def get[T <: Profile]: Option[T] = Some((new SomeProfile).asInstanceOf[T])
}
val foo2: Foo = new Foo {
// This does not compile with type mismatch :(
def get[T <: Profile]: Option[T] = Some(new SomeProfile)
}
}
Compiler says:
type mismatch;
found : Playground.this.SomeProfile
required: T
But SomeProfile
is T
, no?
Update:
I want to implement this trait DatabaseConfigProvider with exact type and do it in this way:
val dc: DatabaseConfig[JdbcProfile] = ???
val prov = new DatabaseConfigProvider {
def get[P <: BasicProfile] = dc.asInstanceOf[DatabaseConfig[P]]
}
which looks ugly because of asInstanceOf
.