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

Getters and Setters

No replies
Samuel Robert Reid
Joined: 2008-12-17,
User offline. Last seen 1 year 22 weeks ago.
Why doesn't the compiler allow you to have a setter without corresponding getter?  For example in this code:
object TestSetter {
  def main(args: Array[String]) = {
    class Time{
      var h=12
      def hour:Int=h
      def hour_=(x:Int)={h=x}
    }
    val t=new Time
    t.hour = 123
  }
}

if I comment out the line
def hour:Int=h
then compilation fails.  Why is this?

Sam Reid

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