tags:

views:

202

answers:

1

This works:

class ButtonCountObserver {
  private var cnt = 0  // private field
  def count = cnt      // reader method
  def count_=(newCount: Int) = cnt = newCount  // writer method
 // ...
}

val b = new ButtonCountObserver 
b.count = 0

But this doesn't

class ButtonCountObserver {
  private var cnt = 0  // private field
  def count_=(newCount: Int) = cnt = newCount  // writer method
 // ...
}

val b = new ButtonCountObserver 
b.count = 0

I get: error: value count is not a member of ButtonCountObserver

Is it possible to create a setter (with the syntactic sugar) without a getter?

+8  A: 

The spec requires that both a setter and getter are defined to be able to use the syntactic sugar for calling the setter:

The interpretation of an assignment to a simple variable x = e depends on the definition of x. If x denotes a mutable variable, then the assignment changes the current value of x to be the result of evaluating the expression e. The type of e is expected to conform to the type of x. If x is a parameterless function defined in some template, and the same template contains a setter function x_= as member, then the assignment x = e is interpreted as the invocation x_=(e ) of that setter function. Analogously, an assignment f .x = e to a parameterless function x is interpreted as the invocation f .x_=(e ). An assignment f (args) = e with a function application to the left of the ‘=’ operator is interpreted as f .update(args, e ), i.e. the invocation of an update function defined by f .

Furthermore, the getter must be visible in order to use the setter. I'm not sure if this is specified

Getter not visible #1

// error: method x cannot be accessed in x.Test
object x {
  class Test { 
    private[this] var x0: Int = 0
    private[Test] def x = x0
    def x_=(a: Int) = x0 = a 
  }
  val t = new Test
  t.x = 1
}

Getter not visible #2

//<console>:11: error: type mismatch; found   : x.Test required: ?{val x: ?}
object x {
  class Test { 
    private[this] var x0: Int = 0
    private[this] def x = x0
    def x_=(a: Int) = x0 = a 
  }
  val t = new Test
  t.x = 1
}

Getter visible

object x {
  class Test { 
    private[this] var x0: Int = 0
    private[x] def x = x0
    def x_=(a: Int) = x0 = a 
  }
  val t = new Test
  t.x = 1
}
retronym
*"I'm not if this is specified"*, "I'm not *sure* if this is specified"?
huynhjl
Exactly. Edited, thanks.
retronym