Why is an accessor required when defining an assignment method?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Why is an accessor required when defining an assignment method?

Owen-2
In

  object foo {
    def x_=(x: Int) { }
  }
  
  foo.x = 2

there is an error, but in

  object foo {
    def x_=(x: Int) { }
    def x = 3
  }
  
  foo.x = 2

there is not. Could this restriction be lifted?

--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why is an accessor required when defining an assignment method?

som-snytt

Requiring the member selection to type check affects implicit conversions.

I'm not sure what I expected here:

scala> class C { def f = 42 }
defined class C

scala> class D { def f_=(i: Int) = println(s"set $i") }
defined class D

scala> class E
defined class E

scala> implicit def cv(d: C): D = new D
warning: there was one feature warning; re-run with -feature for details
cv: (d: C)D

scala> (new C).f = 5
set 5
new C().f: Int = 42

scala> implicit def cv2(d: E): C = new C
warning: there was one feature warning; re-run with -feature for details
cv2: (d: E)C

scala> (new E).f = 5
<console>:16: error: value f_= is not a member of C
       (new E).f = 5
               ^




On Monday, January 11, 2016 at 9:55:31 AM UTC-8, Owen wrote:
In

  object foo {
    def x_=(x: Int) { }
  }
  
  foo.x = 2

there is an error, but in

  object foo {
    def x_=(x: Int) { }
    def x = 3
  }
  
  foo.x = 2

there is not. Could this restriction be lifted?

--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: Why is an accessor required when defining an assignment method?

Owen-2
Huh. That's really weird.

I suppose implicit conversions and mutability will always be a little odd together because implicit conversion naturally creates temporary objects that thwart mutable state.

On Friday, January 15, 2016 at 1:35:21 PM UTC-5, som-snytt wrote:

Requiring the member selection to type check affects implicit conversions.

I'm not sure what I expected here:

scala> class C { def f = 42 }
defined class C

scala> class D { def f_=(i: Int) = println(s"set $i") }
defined class D

scala> class E
defined class E

scala> implicit def cv(d: C): D = new D
warning: there was one feature warning; re-run with -feature for details
cv: (d: C)D

scala> (new C).f = 5
set 5
new C().f: Int = 42

scala> implicit def cv2(d: E): C = new C
warning: there was one feature warning; re-run with -feature for details
cv2: (d: E)C

scala> (new E).f = 5
<console>:16: error: value f_= is not a member of C
       (new E).f = 5
               ^




On Monday, January 11, 2016 at 9:55:31 AM UTC-8, Owen wrote:
In

  object foo {
    def x_=(x: Int) { }
  }
  
  foo.x = 2

there is an error, but in

  object foo {
    def x_=(x: Int) { }
    def x = 3
  }
  
  foo.x = 2

there is not. Could this restriction be lifted?

--
You received this message because you are subscribed to the Google Groups "scala-debate" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.