Scala HashMap with typed entries

I'm not sure if i got the topic right. I'll try to describe the problem. I have one common field trait. StringField and IntField extend this class:

trait BaseField[T] {
  def name = "field"

  var owner : FieldContainer

  var value : T
  def set(value : T) {
    this.value = value
    this.owner.fields.put(name, this)
  }
}


class StringField extends BaseField[String]
class IntField extends BaseField[Int]

How do i implement the FieldContainer class? What i want is to match the FieldTypes later on:

val fieldContainer = {...init code here...}

fieldContainer.fields foreach {
  field -> {
     field match {
       case f: StringField => println("String")
       case f: IntField => println("Int")
       case _ => println("Unknown")
     }
  }
}

This is my FieldContainer (so far)

trait FieldContainer {

  private metaFields : HashMap[String, Any] = new HashMap[String, Any]
  def fields : HashMap[String, Any] = this.metaFields
}

And i use it in that way:

class Pizza extends FieldContainer {

  object name extends StringField(this) {
    override def name = "pizza_name"
  }

  object pieces extends IntField(this) {
     override def name = "pieces_count"
  }
}

Answers


Fields don't need to know their owners.

class BaseField[T](initValue: T, val name: String = "field") {
  private[this] var _value: T = initValue
  def apply() = _value
  def update(v: T) { _value = v }
  override def toString(): String = name + "(" + apply() + ")"
}

class StringField(initValue: String, name: String = "field") extends BaseField[String](initValue, name)
class IntField(initValue: Int, name: String = "field") extends BaseField[Int](initValue, name)

trait FieldContainer {
  protected def addField[C <: BaseField[_]](field: C): C = {
    _fields += (field.name -> field)
    field
  }

  protected def stringField(initValue: String, name: String): StringField =
    addField(new StringField(initValue, name))

  protected def intField(initValue: Int, name: String): IntField =
    addField(new IntField(initValue, name))

  private var _fields : Map[String, Any] = Map[String, Any]()
  def fields : Map[String, Any] = _fields
}

Objects (singletons) initialized when first accessed, so you should use val instead of object for fields:

class Pizza extends FieldContainer {

  val name = stringField("", "pizza_name")

  val pieces = intField(0, "pieces_count")

  val mass: BaseField[Double] = addField(new BaseField[Double](0, "mass"))
}

Usage:

scala> val p = new Pizza()
p: Pizza = Pizza@8c61644

scala> p.fields
res0: Map[String,Any] = Map(pizza_name -> pizza_name(), pieces_count -> pieces_count(0), mass -> mass(0.0))

scala> p.name() = "new name"

scala> p.pieces() = 10

scala> p.mass() = 0.5

scala> p.fields
res4: Map[String,Any] = Map(pizza_name -> pizza_name(new name), pieces_count -> pieces_count(10), mass -> mass(0.5))

scala> p.name()
res5: String = new name

scala> p.pieces()
res6: Int = 10

scala> p.mass
res7: BaseField[Double] = mass(0.5)

Need Your Help

How to stop ActiveX objects automatically changing size in office?

excel vba excel-vba activex

This thread discusses a problem I've been having with ActiveX objects in an Excel spreadsheet. It's a mess to read through and ultimately doesn't have a cohesive answer.

How do I interpret this JVM fault?

java debugging java-native-interface core hs-err

I have a Java app that makes use of some native code, and it's faulting. I want to find out where it's faulting, but I'm not sure how to read the hs_err_pid dump file: