[scala] example of private qualifier

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

[scala] example of private qualifier

thebugslayer
Hi list,

Is there examples out there showing the different usage of private qualifier for [this], [C] and plain private?

More specifically, if I have this class:
class A {
  private var x
  private[this] var y
  private[A] var z
}

Can you give me exmples when to use one over the other?

Thanks,
Zemian

--
A scala web framework http://code.google.com/p/sweetscala
My programming blog http://www.jroller.com/thebugslayer

Reply | Threaded
Open this post in threaded view
|

Re: [scala] example of private qualifier

Jan Lohre
I would guess private and private[A] are the same.

private[this] means private to the instance (not class)
i.e.
class A {
  private[this] f = "a"

  def m(a : A) = {
    a.f //this will not compile
  }
}

private[somepackage] is package private.

I hope this was all right, somebody with more scala experience please correct me if I am wrong.

Kind regards,
Jan

2009/5/16 Zemian Deng <[hidden email]>
Hi list,

Is there examples out there showing the different usage of private qualifier for [this], [C] and plain private?

More specifically, if I have this class:
class A {
  private var x
  private[this] var y
  private[A] var z
}

Can you give me exmples when to use one over the other?

Thanks,
Zemian

--
A scala web framework http://code.google.com/p/sweetscala
My programming blog http://www.jroller.com/thebugslayer


Reply | Threaded
Open this post in threaded view
|

Re: [scala] example of private qualifier

Dean Wampler-2
In reply to this post by thebugslayer
private[this] (equivalent to protected[this]) means that that "y" is only visible to methods in the same instance. For example, you could not reference y on a second instance in an equals method, i.e., "this.y == that.y" would generate a compilation error on "that.y".

private[A] is the same as Java private, you can now see those members from other instances, but not outside the class and not in subclasses. protected[A] is identical. Note that this means that protected[A] != protected. That is, protected[A] var z is still invisible to subclasses.

You can also have private[packageA], which means that the member is visible in the scope of "packageA", which must enclose the class, either immediately or as a parent package, e.g., 

package packageA.packageB.packageC {
  class A {...}
}

Hope this helps.

On Sat, May 16, 2009 at 9:14 AM, Zemian Deng <[hidden email]> wrote:
Hi list,

Is there examples out there showing the different usage of private qualifier for [this], [C] and plain private?

More specifically, if I have this class:
class A {
  private var x
  private[this] var y
  private[A] var z
}

Can you give me exmples when to use one over the other?

Thanks,
Zemian

--
A scala web framework http://code.google.com/p/sweetscala
My programming blog http://www.jroller.com/thebugslayer




--
Dean Wampler
coauthor of "Programming Scala" (O'Reilly)
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
-  http://groups.google.com/group/chicagoscala
-  http://www.meetup.com/chicagoscala/ (Meetings)
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org
Reply | Threaded
Open this post in threaded view
|

Re: [scala] example of private qualifier

Paul Phillips-3
On Sat, May 16, 2009 at 09:26:38AM -0500, Dean Wampler wrote:
> private[this] (equivalent to protected[this])

Untrue, protected[this] allows the subclass instance access.

> private[A] is the same as Java private

Untrue in inner classes.  In java, an inner class's private data is
still accessible from the enclosing class.  In scala private[Inner]
locks out the outer class.

> Note that this means that protected[A] != protected. That is,
> protected[A] var z is still invisible to subclasses.

Also not true, protected and [scope] are additive, so protected[Foo] is
accesible in subclasses as well as inside the Foo boundary.

You have perhaps been led astray by the interpreter, where access does
not work correctly.  Try compiling and running examples of these.

class A {
  protected[A] var x = 5
  protected[this] var y = 10
}
class B extends A { def printSum = println(x + y) }

$ scala -e '(new B).printSum'
15

--
Paul Phillips      | It's not enough to bash in heads - you've got to
Analgesic          | bash in minds.
Empiricist         |     -- Capt Hammer
up hill, pi pals!  |----------* http://www.improving.org/paulp/ *----------
Reply | Threaded
Open this post in threaded view
|

Re: [scala] example of private qualifier

Dean Wampler-2


On Sat, May 16, 2009 at 9:52 AM, Paul Phillips <[hidden email]> wrote:
On Sat, May 16, 2009 at 09:26:38AM -0500, Dean Wampler wrote:
> private[this] (equivalent to protected[this])

Untrue, protected[this] allows the subclass instance access.

True enough, but not that intuitive to me. 


> private[A] is the same as Java private

Untrue in inner classes.  In java, an inner class's private data is
still accessible from the enclosing class.  In scala private[Inner]
locks out the outer class.

True. 


> Note that this means that protected[A] != protected. That is,
> protected[A] var z is still invisible to subclasses.

Also not true, protected and [scope] are additive, so protected[Foo] is
accesible in subclasses as well as inside the Foo boundary.

You have perhaps been led astray by the interpreter, where access does
not work correctly.  Try compiling and running examples of these.

class A {
 protected[A] var x = 5
 protected[this] var y = 10
}
class B extends A { def printSum = println(x + y) }

$ scala -e '(new B).printSum'
15

Doh! You're right. It works as I described in the interpreter, but not when compiled (v2.7.4 and v2.8). Tricksy Scalas!
 


--
Paul Phillips      | It's not enough to bash in heads - you've got to
Analgesic          | bash in minds.
Empiricist         |     -- Capt Hammer
up hill, pi pals!  |----------* http://www.improving.org/paulp/ *----------



--
Dean Wampler
coauthor of "Programming Scala" (O'Reilly)
twitter: @deanwampler, @chicagoscala
Chicago-Area Scala Enthusiasts (CASE):
-  http://groups.google.com/group/chicagoscala
-  http://www.meetup.com/chicagoscala/ (Meetings)
http://www.objectmentor.com
http://www.polyglotprogramming.com
http://www.aspectprogramming.com
http://aquarium.rubyforge.org
http://www.contract4j.org