coder25
coder25

Reputation: 2393

type parameter to take more than one type

I have the below trait and I want constants to have more than one type parameter

trait MyTrait[+A]  {

  val value: A

}

object Currency {

  sealed trait MyConstants extends MyTrait[String]

  case object A extends MyConstants {val value ="abc"}

  //etc.
  case object B extends MyConstants {val value = "def"}

  //etc.

  val list = Seq(A, B)


}

I want to create another case object C extends MyConstants {val value = 10}

Can anyone suggest a way to make a type parameter take Int or String as types

Upvotes: 1

Views: 180

Answers (1)

slouc
slouc

Reputation: 9698

Why not parameterize MyConstants itself?

sealed trait MyConstants[T] extends MyTrait[T]

case object A extends MyConstants[String] {val value ="abc"}

case object B extends MyConstants[Int] {val value = 10}

Upvotes: 2

Related Questions