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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.