I currently have something like this:
case class Bear(a: String, b: String) {
val can: Can[T] = ??
def drink[T](str: String) = can.open(str)
}
I need to modify this to be used for only 4 types A,B,C and D. For example given an instance of Bear we should only be able to call bearinstance.drink[A]("abc")
, bearinstance.drink[B]("abc")
, bearinstance.drink[C]("abc")
and bearinstance.drink[D]("abc")
. Any other type should not be allowed.
Now the question is how do I rewrite this method for specific types?
Another issue is with the can
, assuming I manage to rewrite drink
to be used with only types 'A', 'B', 'C' and 'D', I will have to create can
for all the four types as member variables. How do I make generic method to dynamically select the can
based on the type? One option is to implicitly declare can
outside the class, but it requires class parameters to be declared.
Any leads will be appreciated.