Reputation: 21
Below is the code snippet...a bit testing mode code snippet.I am trying to create a method through which i can change the return type.
While calling the method i am getting the exception "class type expected object found"
trait A
trait B extends A {
def aba[T](a:Int):T
}
class D
class C extends B {
def aba[D](a:Int) = {
println("asasas")
new D
}
}
Upvotes: 0
Views: 123
Reputation: 22374
The actual error in Scala 2.11 is
:13: error: class type required but D found
Which means that you can't instantiate generic type D
- your D
class was shadowed by your [D]
definition
You also can't override T
(as a part of your aba
's method signature) due to Liskov Substitution Principle as it will change behaviour (and signature) for subclass. However you can define T
as type member:
trait A
trait B extends A {
type T
def aba(a:Int):T
}
class D
class C extends B {
type T = D
def aba(a:Int): T = {
println("asasas")
new D
}
}
Upvotes: 3