Skip to content

Commit

Permalink
Add spec and test for private toplevel definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Jan 24, 2019
1 parent 8ced7cc commit 7d1754b
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
19 changes: 18 additions & 1 deletion docs/docs/reference/dropped-features/package-objects.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
layout: doc-page
title: "Instance Definitions"
title: "Dropped: Package Objects"
---

Package objects
Expand Down Expand Up @@ -40,3 +40,20 @@ If a source file `src.scala` contains such toplevel definitions, they will be pu
**Note 2:** A toplevel main method `def main(args: Array[String]): Unit = ...` is wrapped as any other method. If it appears
in a source file `src.scala`, it could be invoked from the command line using a command like `scala src$package`. Since the
"program name" is mangled it is recommended to always put `main` methods in explicitly named objects.

### Private toplevel definitions

If a private toplevel definition is wrapped in a synthetic object, it is visible only
in definitions in the same source file. By contrast, a package-private definition is visible in the whole containing package. Example:

```scala
// file 1:
package p
private val x = 1
private[p] val y = 2

// file 2:
package p
val xx = x // error: not found: x
val yy = y // ok
```
11 changes: 11 additions & 0 deletions tests/neg/toplevel-privates/A.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package p

private val x = 10

val y = x

private[p] val xx = 10

val yy = xx

private class C
11 changes: 11 additions & 0 deletions tests/neg/toplevel-privates/Test.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package p

val x1 = x // error

val y1 = y // ok

val xx1 = xx // ok

val yy1 = yy // ok

val c = new C // ok

0 comments on commit 7d1754b

Please sign in to comment.