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

Only setting a field is forbidden

3 replies
f.esser
Joined: 2009-11-23,
User offline. Last seen 42 years 45 weeks ago.

Is there a reason, why the last line is an error (Scala 2.8), if
def foo= f
is not present.

class Foo {
private var f: String= _
// def foo= f
def foo_= (s: String) = f= s
}

object SnippetTest {

def main(args: Array[String]) = {
val f= new Foo
f.foo_=("oha") // ok!
f.foo= "oha" // error without: def foo= f
}
}

dcsobral
Joined: 2009-04-23,
User offline. Last seen 38 weeks 5 days ago.
Re: Only setting a field is forbidden
Setter syntax only works if there's a getter of the same name. It's in the specification.

On Mon, Nov 23, 2009 at 8:50 AM, f.esser <esser [dot] friedrich [at] gmail [dot] com> wrote:

Is there a reason, why the last line is an error (Scala 2.8), if
 def foo= f
is not present.

class Foo {
 private var f: String= _
//  def foo= f
 def foo_= (s: String) = f= s
}

object SnippetTest {

 def main(args: Array[String]) = {
   val f= new Foo
   f.foo_=("oha")  // ok!
   f.foo= "oha"     // error without: def foo= f
 }
}
--
View this message in context: http://old.nabble.com/Only-setting-a-field-is-forbidden-tp26475135p26475135.html
Sent from the Scala - Debate mailing list archive at Nabble.com.




--
Daniel C. Sobral

Veni, vidi, veterni.
Elazar Leibovich
Joined: 2009-10-07,
User offline. Last seen 42 years 45 weeks ago.
Re: Only setting a field is forbidden
I believe the Esser knew that, but wondered why wouldn't the spec. allow set-only field of an object (for internal use only).

On Mon, Nov 23, 2009 at 3:02 PM, Daniel Sobral <dcsobral [at] gmail [dot] com> wrote:
Setter syntax only works if there's a getter of the same name. It's in the specification.

On Mon, Nov 23, 2009 at 8:50 AM, f.esser <esser [dot] friedrich [at] gmail [dot] com> wrote:

Is there a reason, why the last line is an error (Scala 2.8), if
 def foo= f
is not present.

class Foo {
 private var f: String= _
//  def foo= f
 def foo_= (s: String) = f= s
}

object SnippetTest {

 def main(args: Array[String]) = {
   val f= new Foo
   f.foo_=("oha")  // ok!
   f.foo= "oha"     // error without: def foo= f
 }
}
--
View this message in context: http://old.nabble.com/Only-setting-a-field-is-forbidden-tp26475135p26475135.html
Sent from the Scala - Debate mailing list archive at Nabble.com.




--
Daniel C. Sobral

Veni, vidi, veterni.

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Only setting a field is forbidden

It does. Feel free to make the getter private.

2009/11/23 Elazar Leibovich :
> I believe the Esser knew that, but wondered why wouldn't the spec. allow
> set-only field of an object (for internal use only).
>
> On Mon, Nov 23, 2009 at 3:02 PM, Daniel Sobral wrote:
>>
>> Setter syntax only works if there's a getter of the same name. It's in the
>> specification.
>>
>> On Mon, Nov 23, 2009 at 8:50 AM, f.esser
>> wrote:
>>>
>>> Is there a reason, why the last line is an error (Scala 2.8), if
>>>  def foo= f
>>> is not present.
>>>
>>> class Foo {
>>>  private var f: String= _
>>> //  def foo= f
>>>  def foo_= (s: String) = f= s
>>> }
>>>
>>> object SnippetTest {
>>>
>>>  def main(args: Array[String]) = {
>>>    val f= new Foo
>>>    f.foo_=("oha")  // ok!
>>>    f.foo= "oha"     // error without: def foo= f
>>>  }
>>> }
>>> --
>>> View this message in context:
>>> http://old.nabble.com/Only-setting-a-field-is-forbidden-tp26475135p26475...
>>> Sent from the Scala - Debate mailing list archive at Nabble.com.
>>>
>>
>>
>>
>> --
>> Daniel C. Sobral
>>
>> Veni, vidi, veterni.
>
>

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