- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Uniform Access Principle in Scala 2.9.0.RC2
Fri, 2011-05-27, 01:22
//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!
Fri, 2011-05-27, 01:47
#2
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")
}
}
Fri, 2011-05-27, 01:57
#3
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:
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!
Fri, 2011-05-27, 02:07
#4
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!
Fri, 2011-05-27, 02:17
#5
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!
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!