Lower type bound on Scala field in mutable, covariant class?

懵懂的女人 提交于 2019-12-08 17:13:27

问题


I want to create a covariant class which is mutable, so I need to add a lower type bound to the setter method. But I also want the setter method to set a field, so I guess the field needs to have the same type bound?

class Thing[+F](initialValue: F) {

    private[this] var secondValue: Option[G >: F] = None

    def setSecondValue[G >: F](v: G) = {
        this.secondValue = Some(v)
     }
}

The method compiles fine. But the field called secondValue doesn't compile at all, with the error message:

    Multiple markers at this line
        - ']' expected but '>:' found.
        - not found: type G

What do I need to do?


回答1:


You need the forSome construct, which introduces G as existential type:

class Thing[+F](initialValue: F) {
  private[this] var secondValue: Option[G] forSome { type G >: F} = None

  def setSecondValue[G >: F](v: G) = {
    this.secondValue = Some(v)
  }
}

In your original code for secondValue, G has been pulled out of thin air, i.e., it hasn't been introduced properly. In case of setSecondValue the user (or the compiler) binds G at call site, but for a field that's not an option (especially, since yours is private). Read more about forSome and existential types in Scala here, here or here.




回答2:


@mhs answer is right.

You can also use wildcard syntax (like in java), which has exactly the same meaning:

scala> :paste
// Entering paste mode (ctrl-D to finish)

class Thing[+F](initialValue: F) {
  private[this] var secondValue: Option[_ >: F] = None

  def setSecondValue[G >: F](v: G) = {
    this.secondValue = Some(v)
  }

  def printSecondValue() = println(secondValue)
}

// Exiting paste mode, now interpreting.

defined class Thing

scala> val t = new Thing(Vector("first"))
t: Thing[scala.collection.immutable.Vector[java.lang.String]] = Thing@1099257

scala> t.printSecondValue()
None

scala> t.setSecondValue(Seq("second"))

scala> t.printSecondValue()
Some(List(second))


来源:https://stackoverflow.com/questions/11858910/lower-type-bound-on-scala-field-in-mutable-covariant-class

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!