开发者

Scala: public getter with private setter?

开发者 https://www.devze.com 2023-03-19 08:59 出处:网络
How can I make a field that has a public getter but the setter is private, in Scala? EDIT: too bad there is no one-liner way to do this like there is in Groovy.Those so开发者_Python百科lutions are al

How can I make a field that has a public getter but the setter is private, in Scala?

EDIT: too bad there is no one-liner way to do this like there is in Groovy. Those so开发者_Python百科lutions are all pretty clunky looking.


class Foo {
  private var _value: Int = 0
  def value = _value
}


class ExplicitProperty {
   private[this] var s: Int = _
   def size = s
   private def size_=(x: Int) {
      s = x
   }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消