Access Fields Directly (not using getters)

Hi All,

How can I force Scala to access a field directly and not use a getter method (without using private[this]) ?

See the example

class User(var id: Int, var name: String) {
def doSomething = {
println(id)
}
}

If I decompile the generated Java class, I see that the method doSomething() uses the accessor method id() to get the value of id.

Thanks,

Drew

Re: Access Fields Directly (not using getters)

If you declare the var as private[this] you will get direct field access from within the class, but of course you will not be able to access it from outside the class.

Re: Access Fields Directly (not using getters)

I don't understand why you care? The jvm will optimise out the
function call if it gets used enough to matter...

On 30 December 2011 20:07, Drew Kutcharian wrote:
> Hi All,
>
> How can I force Scala to access a field directly and not use a getter method (without using private[this]) ?
>
>
> See the example
>
> class User(var id: Int, var name: String) {
>        def doSomething = {
>                println(id)
>        }
> }
>
> If I decompile the generated Java class, I see that the method doSomething() uses the accessor method id() to get the value of id.
>
> Thanks,
>
> Drew

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