Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- trait SemiringElement {
- /**
- * The element type
- */
- type E
- /**
- * The type returned by the the addition and multiplication operators
- */
- type R <: SemiringElement
- val value: E
- def +(that: R): R
- def *(that: R): R
- override def toString = value.toString
- }
- case class BooleanSemiringElement(init: Boolean) extends SemiringElement {
- type E = Boolean
- type R = BooleanSemiringElement
- val value = init
- def +(that: BooleanSemiringElement#R) = BooleanSemiringElement(value || that.value)
- def *(that: BooleanSemiringElement#R) = BooleanSemiringElement(value && that.value)
- }
- trait Semiring {
- type E <: SemiringElement
- /**
- * The addition identity
- */
- val zero: E
- /**
- * The multiplication identity
- */
- val one: E
- }
- object BooleanSemiring extends Semiring {
- type E = BooleanSemiringElement
- val zero = BooleanSemiringElement(false)
- val one = BooleanSemiringElement(true)
- }
- class ElementMap(s: Semiring) {
- val m = mutable.Map[String, SemiringElement]()
- }
- val x = new ElementMap(BooleanSemiring)
- scala> val x = new ElementMap(BooleanSemiring)
- x: ElementMap = ElementMap@46cf97b
- scala> x.m
- res2: scala.collection.mutable.Map[String,SemiringElement] = Map()
- scala> x.m("one") = BooleanSemiring.one
- scala> x.m("one") + BooleanSemiring.one
- <console>:12: error: type mismatch;
- found : BooleanSemiring.one.type (with underlying type BooleanSemiring.BooleanSemiringElement)
- required: _1.R where val _1: SemiringElement
- x.m("one") + BooleanSemiring.one
- ^
- class ElementMap[BooleanSemiring]...
- class ElementMap(s: Semiring) {
- val m = mutable.Map[String, s.E]()
- }
- scala> class ElementMap[S <: Semiring](s: S) {
- | val m = collection.mutable.Map[String, S#E]()
- | }
- defined class ElementMap
- scala> val x = new ElementMap(BooleanSemiring)
- x: ElementMap[BooleanSemiring.type] = ElementMap@6544c984
- scala> x.m("one") = BooleanSemiring.one
- scala> x.m("one") + BooleanSemiring.one
- res0: BooleanSemiringElement = true
- scala> class ElementMap[SE <: SemiringElement](s: Semiring { type E = SE }) {
- | val m = collection.mutable.Map[String, SE]()
- | }
- defined class ElementMap
- scala> val x = new ElementMap(BooleanSemiring)
- x: ElementMap[BooleanSemiringElement] = ElementMap@4cf353e5
- scala> x.m("one") = BooleanSemiring.one
- scala> x.m("one") + BooleanSemiring.one
- res1: BooleanSemiringElement = true
Add Comment
Please, Sign In to add comment