Skip to content

Mixing in support for other types

hunterpayne edited this page Jan 15, 2019 · 2 revisions

There are currently 3 jars provided by eprop: core, eprop-squants, and eprop-joda. The code is separated into its own jars to prevent unnecessary leaking of extra libraries into your classpath. To use types from Squants or Joda you add the jar to your build environment and then add an extra import to your code and that's it.

import org.eprop.EKey._
// extra import that we add for Squants support
import org.eprop.EKeySquants._
// imports from squants

object MassPType extends EKeyType[Mass]('mass)
object LengthPType extends EKeyType[Length]('length)
object AreaPType extends EKeyType[Area]('area)

// declare your extensible model type
class Container(props: EProperty[_]*) extends Extensible {

  merge(Seq(Kilograms(10) as MassPType)) // a default
  merge(props)

  def mass: Option[Mass] = get[Mass](MassPType)
  def length: Option[Length] = get[Length](LengthPType)
  def area: Option[Area] = get[Area](AreaPType)
}

// now make an instance of your container with some values
val container = new Container(
  Grams(10) as MassPType, Meters(10.0) as LengthPType, 
  SquareMeters(15.2) as AreaPType)

assert(container.mass == Some(Kilograms(0.01)))
assert(container.length == Some(Meters(10.0)))
assert(container.area == Some(SquareMeters(15.2)))
Clone this wiki locally