This page is no longer maintained — Please continue to the home page at www.scala-lang.org

About mutable objects

2 replies
Jean-Marie Galliot
Joined: 2010-01-13,
User offline. Last seen 42 years 45 weeks ago.
Hi,

I am posting a few examples on my blog to share my enthusiasm about Scala.

When testing one of the examples, I was surprised by an unexpected behavior (for me).

This code works as expected:


> class Product (var insuranceAmount: Int,
> deductible: Int) {
>   private var internal_rate = 0.05
>   def rate = internal_rate
>   def rate_= (r: Double) {internal_rate = r}
> }
>
> object Main {
>   def main(args: Array): Unit = {
>     val p1 = new Product(10000, 500)
>     p1.rate = 0.06
>   }
> }


But if I comment out the rate accessor method,
like this:


> class Product (var insuranceAmount: Int,
> deductible: Int) {
>   private var internal_rate = 0.05
>   // def rate = internal_rate
>   def rate_= (r: Double) {internal_rate = r}
> }
>
> object Main {
>   def main(args: Array): Unit = {
>     val p1 = new Product(10000, 500)
>     p1.rate = 0.06  // compile error here !
>     p1.rate_=(0.06) // but this is valid !
>   }
> }


I got a compile error on the mutator method call.
Does it mean that we cannot use this form of
method ending with _=, if there is no
corresponding accessor method? It looks
inconsistent to me.

Thank you

Jean-Marie Galliot

http://jmgalliot.wordpress.com


Randall R Schulz
Joined: 2008-12-16,
User offline. Last seen 1 year 29 weeks ago.
Re: About mutable objects

On Wednesday January 13 2010, Jean-Marie Galliot wrote:
> Hi,
>
> ...
> Does it mean that we cannot use this form of
> method ending with _=, if there is no
> corresponding accessor method? It looks
> inconsistent to me.

Is something a property of the class if its value cannot be observed
from outside?

> Thank you
>
> Jean-Marie Galliot

Randall Schulz

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: About mutable objects
The syntactic sugar for "prop_=" is only used if "prop" exists as well. It's spec'ed that way. I don't recall having heard of the reason for that, but I'm pretty much in favor of the uniform access principle theory.
Basically, if you can use "prop" to see some property, and that property is mutable, then you can use "prop = ..." to mutate it. If there's no "prop" visible, then there's no reason to provide an "prop = ...".

On Wed, Jan 13, 2010 at 7:13 PM, Jean-Marie Galliot <jean [dot] marie [dot] galliot [at] gmail [dot] com> wrote:
Hi,

I am posting a few examples on my blog to share my enthusiasm about Scala.

When testing one of the examples, I was surprised by an unexpected behavior (for me).

This code works as expected:


> class Product (var insuranceAmount: Int,
> deductible: Int) {
>   private var internal_rate = 0.05
>   def rate = internal_rate
>   def rate_= (r: Double) {internal_rate = r}
> }
>
> object Main {
>   def main(args: Array): Unit = {
>     val p1 = new Product(10000, 500)
>     p1.rate = 0.06
>   }
> }


But if I comment out the rate accessor method,
like this:


> class Product (var insuranceAmount: Int,
> deductible: Int) {
>   private var internal_rate = 0.05
>   // def rate = internal_rate
>   def rate_= (r: Double) {internal_rate = r}
> }
>
> object Main {
>   def main(args: Array): Unit = {
>     val p1 = new Product(10000, 500)
>     p1.rate = 0.06  // compile error here !
>     p1.rate_=(0.06) // but this is valid !
>   }
> }


I got a compile error on the mutator method call.
Does it mean that we cannot use this form of
method ending with _=, if there is no
corresponding accessor method? It looks
inconsistent to me.

Thank you

Jean-Marie Galliot

http://jmgalliot.wordpress.com





--
Daniel C. Sobral

I travel to the future all the time.

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland