0

I used to develop on java, but now I stucked with one thing.

I have some kind of flexible value system.

trait Value[T] {
  def get:T
}

I have implementations of this, for example

class StringValue(value : String) extends Value[String] {
  override def get : String = value
}

class NumberValue(value : Int) extends Value[Int] {
  override def get: Int = value
}

Then, I'm passing Value to Map, but I don't need parameter, as I don't know which implementation should be used.

case class Foo(attributes: Map[Attribute, Value)

On this step I get an error, because Value should have parameter.

I understand that it's might be fundamental difference between Java and Scala, so what should I use in this case?

green-creeper
  • 316
  • 3
  • 15

2 Answers2

1

You can use existential types: Map[Attribute, Value[_]] to express this, but they aren't trivial to use.

I understand that it's might be fundamental difference between Java and Scala

It isn't: in Java you can write Map<Attribute, Value>, but you shouldn't. Instead, you want Map<Attribute, Value<?>> which is more limited than Scala, but expresses the same idea.

Community
  • 1
  • 1
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
0

You can try something along the lines of

case class Foo[K, V](attributes: Map[K, V])

You need to have the class have generic typed parameters in order to be able to use generic parameters.

TheM00s3
  • 3,677
  • 4
  • 31
  • 65