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

Uniform Access Principle in Scala 2.9.0.RC2

5 replies
savamilovanovic
Joined: 2011-05-27,
User offline. Last seen 1 year 22 weeks ago.

//Hello,
//While reading "Programming Scala", Chapter 6, I learned next two
definitions functionally equivalent:

//def 1:
/*
class UniformAccessCounter {
var count = 0
}
*/
//def 2:
class UniformAccessCounter {
private var cnt = 0
def count_(newCount : Int) = {
cnt = newCount
}
def count = cnt
}
//However, next code won't compile:
object Main {
def main(args: Array[String]): Unit = {
var uac = new UniformAccessCounter
val oldCount = uac.count
uac.count_(5) //OK, compiles
uac.count = 5 // ERROR - value count_= is not a member of
AdvOOP.UniformAccessCounter, - reassignment to val
val newCount = uac.count
println (newCount + " == 5 and " + oldCount + " == 0")
}
}
//From the book, I got impression that above line should compile -
could somebody point out where is my mistake?
//Thanks in advance!

Joshua.Suereth
Joined: 2008-09-02,
User offline. Last seen 32 weeks 5 days ago.
Re: Uniform Access Principle in Scala 2.9.0.RC2

Try def count_=(...) = ....

On May 26, 2011 8:22 PM, "Sava" <savamilovanovic@yahoo.com> wrote:
> //Hello,
> //While reading "Programming Scala", Chapter 6, I learned next two
> definitions functionally equivalent:
>
> //def 1:
> /*
> class UniformAccessCounter {
> var count = 0
> }
> */
> //def 2:
> class UniformAccessCounter {
> private var cnt = 0
> def count_(newCount : Int) = {
> cnt = newCount
> }
> def count = cnt
> }
> //However, next code won't compile:
> object Main {
> def main(args: Array[String]): Unit = {
> var uac = new UniformAccessCounter
> val oldCount = uac.count
> uac.count_(5) //OK, compiles
> uac.count = 5 // ERROR - value count_= is not a member of
> AdvOOP.UniformAccessCounter, - reassignment to val
> val newCount = uac.count
> println (newCount + " == 5 and " + oldCount + " == 0")
> }
> }
> //From the book, I got impression that above line should compile -
> could somebody point out where is my mistake?
> //Thanks in advance!
savamilovanovic
Joined: 2011-05-27,
User offline. Last seen 1 year 22 weeks ago.
Re: Uniform Access Principle in Scala 2.9.0.RC2

Thanks for answer - I tried - but next one didn't work either:

class UniformAccessCounter {
private var cnt = 0
def count_(newCount:Int)=cnt=newCount
def count = cnt
}

object Main {
def main(args: Array[String]): Unit = {
var uac = new UniformAccessCounter
val oldCount = uac.count
uac.count_(5) //OK, compiles
uac.count = 5 // ERROR - value count_= is not a member of
AdvOOP.UniformAccessCounter, - reassignment to val
val newCount = uac.count
println (newCount + " == 5 and " + oldCount + " == 0")
}
}

Nilanjan Raycha...
Joined: 2009-11-06,
User offline. Last seen 42 years 45 weeks ago.
Re: Uniform Access Principle in Scala 2.9.0.RC2
I think you are missing =. You count method should look like following
  def count_=(newCount : Int) = {    cnt = newCount  }
Nilanjan

On Thu, May 26, 2011 at 8:22 PM, Sava <savamilovanovic@yahoo.com> wrote:
//Hello,
//While reading "Programming Scala", Chapter 6, I learned next two
definitions functionally equivalent:

//def 1:
/*
class UniformAccessCounter {
       var count = 0
}
*/
//def 2:
class UniformAccessCounter {
       private var cnt = 0
       def count_(newCount : Int) = {
         cnt = newCount
       }
       def count = cnt
}
//However, next code won't compile:
object Main {
   def main(args: Array[String]): Unit = {
     var uac = new UniformAccessCounter
     val oldCount = uac.count
     uac.count_(5) //OK, compiles
     uac.count = 5 // ERROR - value count_= is not a member of
AdvOOP.UniformAccessCounter, - reassignment to val
     val newCount = uac.count
     println (newCount + " == 5 and " + oldCount + " == 0")
   }
}
//From the book, I got impression that above line should compile -
could somebody point out where is my mistake?
//Thanks in advance!

Peter C. Chapin 2
Joined: 2011-01-07,
User offline. Last seen 42 years 45 weeks ago.
RE: Uniform Access Principle in Scala 2.9.0.RC2

I believe you must name the method 'count_=' not 'count_' If you look
carefully you'll see that the error message you're getting says this. The
special characters in strange places does make it easy to miss.

See Chapter 18 in PiS2.

Peter

> -----Original Message-----
> From: scala-language@googlegroups.com [mailto:scala-
> language@googlegroups.com] On Behalf Of Sava
> Sent: Thursday, May 26, 2011 20:22
> To: scala-language
> Subject: [scala-language] Uniform Access Principle in Scala 2.9.0.RC2
>
> //Hello,
> //While reading "Programming Scala", Chapter 6, I learned next two
> definitions functionally equivalent:
>
> //def 1:
> /*
> class UniformAccessCounter {
> var count = 0
> }
> */
> //def 2:
> class UniformAccessCounter {
> private var cnt = 0
> def count_(newCount : Int) = {
> cnt = newCount
> }
> def count = cnt
> }
> //However, next code won't compile:
> object Main {
> def main(args: Array[String]): Unit = {
> var uac = new UniformAccessCounter
> val oldCount = uac.count
> uac.count_(5) //OK, compiles
> uac.count = 5 // ERROR - value count_= is not a member of
> AdvOOP.UniformAccessCounter, - reassignment to val
> val newCount = uac.count
> println (newCount + " == 5 and " + oldCount + " == 0")
> }
> }
> //From the book, I got impression that above line should compile - could
> somebody point out where is my mistake?
> //Thanks in advance!

savamilovanovic
Joined: 2011-05-27,
User offline. Last seen 1 year 22 weeks ago.
Re: Uniform Access Principle in Scala 2.9.0.RC2

That's it! I was defining count_, not count_= as I should have been!
Thanks a lot to all of you!

On May 26, 5:50 pm, "Peter C. Chapin" wrote:
> I believe you must name the method 'count_=' not 'count_' If you look
> carefully you'll see that the error message you're getting says this. The
> special characters in strange places does make it easy to miss.
>
> See Chapter 18 in PiS2.
>
> Peter
>
>
>
>
>
>
>
> > -----Original Message-----
> > From: scala-language@googlegroups.com [mailto:scala-
> > language@googlegroups.com] On Behalf Of Sava
> > Sent: Thursday, May 26, 2011 20:22
> > To: scala-language
> > Subject: [scala-language] Uniform Access Principle in Scala 2.9.0.RC2
>
> > //Hello,
> > //While reading "Programming Scala", Chapter 6, I learned next two
> > definitions functionally equivalent:
>
> > //def 1:
> > /*
> > class UniformAccessCounter {
> >    var count = 0
> > }
> > */
> > //def 2:
> > class UniformAccessCounter {
> >    private var cnt = 0
> >    def count_(newCount : Int) = {
> >      cnt = newCount
> >    }
> >    def count = cnt
> > }
> > //However, next code won't compile:
> > object Main {
> >     def main(args: Array[String]): Unit = {
> >       var uac = new UniformAccessCounter
> >       val oldCount = uac.count
> >       uac.count_(5) //OK, compiles
> >       uac.count = 5 // ERROR - value count_= is not a member of
> > AdvOOP.UniformAccessCounter, - reassignment to val
> >       val newCount = uac.count
> >       println (newCount + " == 5 and " + oldCount + " == 0")
> >     }
> > }
> > //From the book, I got impression that above line should compile - could
> > somebody point out where is my mistake?
> > //Thanks in advance!

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