Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala: public getter with private setter?

Tags:

scala

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 solutions are all pretty clunky looking.

like image 930
Alex Baranosky Avatar asked Jul 10 '11 13:07

Alex Baranosky


2 Answers

class Foo {
  private var _value: Int = 0
  def value = _value
}
like image 71
Jean-Philippe Pellet Avatar answered Oct 17 '22 00:10

Jean-Philippe Pellet


class ExplicitProperty {
   private[this] var s: Int = _
   def size = s
   private def size_=(x: Int) {
      s = x
   }
}
like image 37
agilesteel Avatar answered Oct 17 '22 00:10

agilesteel