Question

I need some help on this scenario:

A trait defines an shared interface with some methods. One of the methods should look like this

 def fromConfig( config : Config): 

Each implementation should override the method with something like:

  override def fromConfig(config: SubConfigA)

However, doing so gives "Method .. overrides nothing" error
even though the config stuff is defined as

  abstract class Config
  trait SubConfigA extends Config
  trait SubConfigB extends Config 
  ....

I remember, in Java you actually can define an interface as parameter, and use sub-interfaces in subclasses for refinement.

Apparently the same does not work in Scala.

The code above is just a draft but the idea is passing specific configurations matching to a specific implementation of the shared interface.

Any idea or advice would be most appreciated.

Thank you

Was it helpful?

Solution

Is this what you want?

abstract class Config
trait ConfigA extends Config
trait ConfigB extends Config

trait FromConfigLoader[C <: Config] {
  def fromConfig(config: C)
}

class TestA extends FromConfigLoader[ConfigA] {
 def fromConfig(config: ConfigA) = ???
}

class TestB extends FromConfigLoader[ConfigB] {
 def fromConfig(config: ConfigB) = ???
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top