lundi 14 décembre 2020

Passing Parameters Made in a Case Class to a Trait

Context

For the sake of being able to change the config for testing, I have a trait with almost all the code. Then in a case class I am trying to pass in the config. In my testing I have a testing case class.

This works (but is not the funcionality I want)

trait Greeting(val name: String) {
  def msg = s"How are you, $name"
}

class prodEnvClass extends Greeting("Production Env") {
  println(msg)
}

class testEnvClass extends Greeting("Testing Env") {
  println(msg)
}

This does not work (but is the functionality I want)

trait Greeting(val name: String) {
  def msg = s"How are you, $name"
}

class prodEnvClass extends Greeting(myEnv) {
  val myEnv = fetchProdEnv()
  println(msg)
}

class testEnvClass extends Greeting(myEnv) {
  val myEnv = fetchTestEnv()
  println(msg)
}

Question

How do I pass a parameter made inside the case class body into the trait? The code that works does not match my use case. I want to use code inside the case class to make the input to the trait.

Aucun commentaire:

Enregistrer un commentaire