Can I make "public val" but "private var" in Scala in one line?
Asked Answered
V

3

10

I.e. Is it possible to make a var that is not assignable from outside of the class ?

Vicious answered 14/2, 2011 at 0:33 Comment(0)
K
14

Right now, no, there's no way to do that.

You're limited to the following three-line solution:

class Hider {
  private[this] var xHidden: Int = 0
  def x = xHidden
  private def x_=(x0: Int) { xHidden = x0 }
}

Now the class itself is the only one who can manipulate the underlying field xHidden, while other instances of the class can use the setter method and everyone can see the getter method.

If you don't mind using different names, you can just make the var private and forget the setter (two lines).

There's no "var to me, val to them" keyword.

Kunin answered 14/2, 2011 at 0:49 Comment(2)
Was the possibility of such keyword discussed in Scala community?Chkalov
@Łukasz - Not while I've been listening, not where I've been listening. (I don't listen everywhere and I've not been around forever, though.) But I suspect that it would not receive much favor, since there is a strong emphasis on keeping the language definition small (i.e. few special cases, even if the cases you do have give you great expressive power), and on favoring immutable solutions over mutable ones.Kunin
S
4

You could do something like:

class Test {
   private var myprivatevar = ""

   def publicvar = myprivatevar
}

From the other classes, you would be able to only use publicvar and as there is no publicvar_= method, you can't assign to it from outside.

Sophist answered 14/2, 2011 at 0:38 Comment(3)
Do you want to say that it is not possible to do it with one modifier?Chkalov
Also in your solution I have to come up with different names for var and def, dont I?Chkalov
You can't reuse the name (in my case, myprivatevar) as Scala implicitly creates a (in this case, private) getter and setter for the variable; if you try to do a def myprivatevar = this.myprivatevar you'd get a name clash as the method is defined twice.Sophist
D
-1

You certainly make something a var and then make it private to the class defining the field.

scala> class Holder(private var someValue: String) {       
     | def getValueOfOther(other: Holder) = other.someValue
     | def combinedWith(holder: Holder) = new Holder(holder1.someValue + " " + holder2.someValue)
     | def value = someValue
     | }
defined class Holder

scala> val holder1 = new Holder("foo")                                                   
holder1: Holder = Holder@1303368e

scala> val holder2 = new Holder("bar")                                                   
holder2: Holder = Holder@1453ecec

scala> holder2.getValueOfOther(holder1)                                                  
res5: String = foo

scala> val holder3 = holder1 combinedWith holder2
holder3: Holder = Holder@3e2f1b1a

scala> holder3.value
res6: String = foo bar
Disloyalty answered 14/2, 2011 at 5:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.