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

will implicit getters/setters stay?

3 replies
Szymon Jachim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Hello,
  I was curious about Java bytecode generated for private fields and inner classes and discovered that for every val/var (even private) Scala compiler is generating a public getter and setter (for vars).
  Are there any plans for removing this? Was this implemented intentionally or is it just a side effect of other decisions?

No. I'm not concerned about isolation or executions speed. :-)
Actually I want to abuse it to experiment with instrumenting classes, detecting field read/write... (OnFieldModified event?) through runtime bytecode manipulation.

// javap output for class A with field a

public final void A$$a_$eq(int);
  Signature: (I)V
  Code:
   0:   aload_0
   1:   iload_1
   2:   putfield        #13; //Field A$$a:I
   5:   return

public final int A$$a();
  Signature: ()I
  Code:
   0:   aload_0
   1:   getfield        #13; //Field A$$a:I
   4:   ireturn


Szymon
ijuma
Joined: 2008-08-20,
User offline. Last seen 22 weeks 2 days ago.
Re: will implicit getters/setters stay?

Szymon Jachim gmail.com> writes:
> Hello,  I was curious about Java bytecode generated for private fields and
> inner classes and discovered that for every val/var (even private) Scala
> compiler is generating a public getter and setter (for vars).

Note that it doesn't generate them if you use private[this] instead of private.

Ismael

Szymon Jachim
Joined: 2008-12-17,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: will implicit getters/setters stay?
Auch. There are so many meanings of "private" in programming languages... Thanks. :-)

On Wed, Dec 17, 2008 at 7:28 PM, Ismael Juma <mlists [at] juma [dot] me [dot] uk> wrote:
Szymon Jachim <sjachim <at> gmail.com> writes:
> Hello,  I was curious about Java bytecode generated for private fields and
> inner classes and discovered that for every val/var (even private) Scala
> compiler is generating a public getter and setter (for vars).

Note that it doesn't generate them if you use private[this] instead of private.

Ismael


odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: will implicit getters/setters stay?

On Wed, Dec 17, 2008 at 7:06 PM, Szymon Jachim <sjachim [at] gmail [dot] com> wrote:
> Hello,
> I was curious about Java bytecode generated for private fields and inner
> classes and discovered that for every val/var (even private) Scala compiler
> is generating a public getter and setter (for vars).

Not every private method generates a public accessor. A public
accessor (with a mangled name) is only generated when necessary, for
instance

- when it is in a trait
- or when it is accessed by some inner class

Cheers

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