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

scope of protection

5 replies
normen.mueller
Joined: 2008-10-31,
User offline. Last seen 3 years 8 weeks ago.

He,

how can one set a class to package private?

For example:

package foo.bar
private[bar] MyClass

I want to have MyClass only be visible inside the package "foo.bar".
But if, for example, this class has been compiled into a jar file and
this jar file is used by another project on could simple create a sub-
package "foo.bar.faz" and thus gain access to "MyClass". Is there a
way to omit such approaches? I know that it is always possible to
gain such access via reflection, but I neglect this (bad) approach.

Cheers,
--
Normen Müller

normen.mueller
Joined: 2008-10-31,
User offline. Last seen 3 years 8 weeks ago.
Re: scope of protection

I just tried the scope of protection example out of the scala book:

package bobsrockets {
package navigation {
private[bobsrockets] class Navigator {
protected[navigation] def useStarChart() {}
class LegOfJourney {
private[Navigator] val distance = 100
}
private[this] var speed = 200
}
}

package launch {
import navigation._
object Vehicle {
private[launch] val guide = new Navigator
}
}
}

package bobsrockets.cheat {
import navigation._
object Cheating {
val cheater = new Navigator
}
}

With the creation of the package "bobsrockets.cheat" I gain access to
"Navigator". So what is the purpose of such scoping "protection" if
it doesn't protect the usage of such a class outside the package?

What am I missing here?

On Jan 23, 2009, at 6:58 PM, Normen Müller wrote:

> He,
>
> how can one set a class to package private?
>
> For example:
>
> package foo.bar
> private[bar] MyClass
>
> I want to have MyClass only be visible inside the package
> "foo.bar". But if, for example, this class has been compiled into a
> jar file and this jar file is used by another project on could
> simple create a sub-package "foo.bar.faz" and thus gain access to
> "MyClass". Is there a way to omit such approaches? I know that it
> is always possible to gain such access via reflection, but I neglect
> this (bad) approach.
>
> Cheers,
> --
> Normen Müller
>
>
>

Cheers,
--
Normen Müller

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 3 days ago.
Re: Re: scope of protection
Make it an object instead of a package.

On Fri, Jan 23, 2009 at 10:38 AM, Normen Müller <normen [dot] mueller [at] googlemail [dot] com> wrote:
I just tried the scope of protection example out of the scala book:

package bobsrockets {
 package navigation {
   private[bobsrockets] class Navigator {
     protected[navigation] def useStarChart() {}
     class LegOfJourney {
       private[Navigator] val distance = 100
     }
     private[this] var speed = 200
   }
 }

 package launch {
   import navigation._
   object Vehicle {
     private[launch] val guide = new Navigator
   }
 }
}

package bobsrockets.cheat {
 import navigation._
 object Cheating {
   val cheater = new Navigator
 }
}

With the creation of the package "bobsrockets.cheat" I gain access to "Navigator".  So what is the purpose of such scoping "protection" if it doesn't protect the usage of such a class outside the package?

What am I missing here?


On Jan 23, 2009, at 6:58 PM, Normen Müller wrote:

He,

how can one set a class to package private?

For example:

package foo.bar
private[bar] MyClass

I want to have MyClass only be visible inside the package "foo.bar".  But if, for example, this class has been compiled into a jar file and this jar file is used by another project on could simple create a sub-package "foo.bar.faz" and thus gain access to "MyClass".  Is there a way to omit such approaches?  I know that it is always possible to gain such access via reflection, but I neglect this (bad) approach.

Cheers,
--
Normen Müller





Cheers,
--
Normen Müller




normen.mueller
Joined: 2008-10-31,
User offline. Last seen 3 years 8 weeks ago.
Re: Re: scope of protection

On Jan 23, 2009, at 8:47 PM, Jorge Ortiz wrote:
> Make it an object instead of a package.

What should I change to an object rather than a package?

Actually, I want to have a class or object that is only visible within
a package. No sub-packages and no sub-classes should gain access. For
example, I have a class "Library". Now I want to set up a couple of
implicit conversion just used by the "Library" class.

One option is to create a companion object, but unfortunately this
companion has to be defined in the same scala file _before_ the
class. Not to go into to much details, but this is sth. I don't want,
among other, cause the number of implicit conversions are quite a
lot. So what I would also accept is the definition of the companion
object after the class, but this is not accepted by the compiler. The
object containing the implicits has to be defined before the
respective class.

Any ideas/ examples?

>
>
> On Fri, Jan 23, 2009 at 10:38 AM, Normen Müller > wrote:
> I just tried the scope of protection example out of the scala book:
>
> package bobsrockets {
> package navigation {
> private[bobsrockets] class Navigator {
> protected[navigation] def useStarChart() {}
> class LegOfJourney {
> private[Navigator] val distance = 100
> }
> private[this] var speed = 200
> }
> }
>
> package launch {
> import navigation._
> object Vehicle {
> private[launch] val guide = new Navigator
> }
> }
> }
>
> package bobsrockets.cheat {
> import navigation._
> object Cheating {
> val cheater = new Navigator
> }
> }
>
> With the creation of the package "bobsrockets.cheat" I gain access
> to "Navigator". So what is the purpose of such scoping "protection"
> if it doesn't protect the usage of such a class outside the package?
>
> What am I missing here?
>
>
>
> On Jan 23, 2009, at 6:58 PM, Normen Müller wrote:
>
> He,
>
> how can one set a class to package private?
>
> For example:
>
> package foo.bar
> private[bar] MyClass
>
> I want to have MyClass only be visible inside the package
> "foo.bar". But if, for example, this class has been compiled into a
> jar file and this jar file is used by another project on could
> simple create a sub-package "foo.bar.faz" and thus gain access to
> "MyClass". Is there a way to omit such approaches? I know that it
> is always possible to gain such access via reflection, but I neglect
> this (bad) approach.
>
> Cheers,
> --
> Normen Müller
>
>
>
>
>
> Cheers,
> --
> Normen Müller
>
>
>
>

Cheers,
--
Normen Müller

Jorge Ortiz
Joined: 2008-12-16,
User offline. Last seen 29 weeks 3 days ago.
Re: Re: scope of protection
I modified the rockets example a little bit.

The johnsrocket.scala file has two compile errors, but you'll have to comment out the first to see the second. You can think of "bobs.rockets" (and "bobs.rockets.navigation") as a package, except it's really a singleton. The downside is that it must be defined in a single file, you can't "re-open" it and put more stuff in it later.

// bobsrockets.scala

package bobs {
  object rockets {
   object navigation {
     private[rockets] class Navigator {
       protected[navigation] def useStarChart() {}
       class LegOfJourney {
         private[Navigator] val distance = 100
       }
       private[this] var speed = 200
     }
   }

   object launch {
     import navigation._
     object Vehicle {
       private[launch] val guide = new Navigator
     }
   }
  }
}

// johnsrockets.scala

package johns {
  import bobs.rockets.navigation._
  object Fail extends Navigator
}

package bobs.rockets.cheat {
  import navigation._
  object Fail extends Navigator
}

--j

On Fri, Jan 23, 2009 at 12:05 PM, Normen Müller <normen [dot] mueller [at] googlemail [dot] com> wrote:
On Jan 23, 2009, at 8:47 PM, Jorge Ortiz wrote:
Make it an object instead of a package.

What should I change to an object rather than a package?

Actually, I want to have a class or object that is only visible within a package.  No sub-packages and no sub-classes should gain access. For example, I have a class "Library".  Now I want to set up a couple of implicit conversion just used by the "Library" class.

One option is to create a companion object, but unfortunately this companion has to be defined in the same scala file _before_ the class.  Not to go into to much details, but this is sth. I don't want, among other, cause the number of implicit conversions are quite a lot.  So what I would also accept is the definition of the companion object after the class, but this is not accepted by the compiler.  The object containing the implicits has to be defined before the respective class.

Any ideas/ examples?




On Fri, Jan 23, 2009 at 10:38 AM, Normen Müller <normen [dot] mueller [at] googlemail [dot] com> wrote:
I just tried the scope of protection example out of the scala book:

package bobsrockets {
 package navigation {
  private[bobsrockets] class Navigator {
    protected[navigation] def useStarChart() {}
    class LegOfJourney {
      private[Navigator] val distance = 100
    }
    private[this] var speed = 200
  }
 }

 package launch {
  import navigation._
  object Vehicle {
    private[launch] val guide = new Navigator
  }
 }
}

package bobsrockets.cheat {
 import navigation._
 object Cheating {
  val cheater = new Navigator
 }
}

With the creation of the package "bobsrockets.cheat" I gain access to "Navigator".  So what is the purpose of such scoping "protection" if it doesn't protect the usage of such a class outside the package?

What am I missing here?



On Jan 23, 2009, at 6:58 PM, Normen Müller wrote:

He,

how can one set a class to package private?

For example:

package foo.bar
private[bar] MyClass

I want to have MyClass only be visible inside the package "foo.bar".  But if, for example, this class has been compiled into a jar file and this jar file is used by another project on could simple create a sub-package "foo.bar.faz" and thus gain access to "MyClass".  Is there a way to omit such approaches?  I know that it is always possible to gain such access via reflection, but I neglect this (bad) approach.

Cheers,
--
Normen Müller





Cheers,
--
Normen Müller






Cheers,
--
Normen Müller




normen.mueller
Joined: 2008-10-31,
User offline. Last seen 3 years 8 weeks ago.
Re: Re: scope of protection

Thanks for your nice example Jorge, I got it!

Cheers,
--
Normen Müller

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