3

I am trying to specify that val constant is visible only to one object:

object Config {
    private[my.pack.MyObject] val Some = Option("String")
}

// in package my.pack
object MyObject {
    val Other = Config.Some
}

While compiling this I get an error:

[error] C:\path\Config.scala:17: ']' expected but '.' found.
[error]   private[my.pack.MyObject] val Some = Option("String")
[error]              ^

What is wrong? As I read about access qualifiers they can be a class or object, not a package, am I wrong?

2
  • So you want to restrict access to val Some to an object other than the assigning object? I may be just confused, but should Config be able to assign a value to Some when it has no access to it? Can you explain a bit more about the use case? Commented Aug 30, 2015 at 9:00
  • 1
    That would be pretty weird if you could do scoping such that the Config object doesn't have access to its own val. Commented Aug 30, 2015 at 9:38

1 Answer 1

6

From the Scala Language specification, section 5.2.1:

A private modifier can be qualified with an identifier C (e.g. private[C]) that must denote a class or package enclosing the definition. Members labeled with such a modifier are accessible respectively only from code inside the package C or only from code inside the class C and its companion module.

(bold mine -- note that "class or package" also admits "object", not sure if this is standard language in the spec, or a bug in the spec)

So your example does not compile because my.pack.MyObject is not an enclosing declaration of val Some.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.