0

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
  }
}
0

1 Answer 1

3

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
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.