Skip to content

Empty package object #10927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
som-snytt opened this issue Jun 4, 2018 · 3 comments
Closed

Empty package object #10927

som-snytt opened this issue Jun 4, 2018 · 3 comments
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) package objects

Comments

@som-snytt
Copy link

som-snytt commented Jun 4, 2018

If I create a package object in the empty package:

object `package` {
  implicit class sss(s: String) {
    def d: Double = s.toDouble
  }
}

then I have to import its members explicitly:

import `package`._

package x {
  object X { def f = "3.14".d }
}

If the empty package is allowed to have a package object, then the workaround also bends the rule that members of the empty package cannot be imported.

Edit: obviously this is more interesting if they're importing in a named package. Also, same behavior on dotty.

@som-snytt
Copy link
Author

som-snytt commented Jan 3, 2020

It's not obvious that members of the empty package are visible to members of packagings, although ordinary lexical scoping seems to suggest obviously yes:

object `package` {
  type MyString = String
}

class Limbo

package p {
  class C {
    val s: MyString = "hello, world"
    val limbo = new Limbo
  }
}
package q {
  import scala.util.chaining._

  object Test extends App {
    println(new p.C().pipe(c => (c.s, c.limbo)))
  }
}

In separate files, the class is visible to p.C but not MyString.

All this is contrary to Java, where members of the empty package are visible only to other members of the empty package, which is a kind of scratch space for experiments but must never accidentally leak into real code.

Similarly, the following works in one file but not two compiled together but not separately:

object `package` {
  implicit class sss(s: String) {
    def d: Double = s.toDouble
  }
}
class Limbo {
  def f = "3.14".d
}

Not sure if that was the point of the original issue, but it's clearly wrong.

It seems to me there is nothing extraordinary about a package object for the empty package. The problem is that access to the empty package is bungled in a couple of ways, both to top-level members and members of the package object.

Spec says: Top-level definitions outside a packaging are assumed to be injected into a special empty package. That package cannot be named and therefore cannot be imported. However, members of the empty package are visible to each other without qualification.

I wonder what doti says.

@som-snytt
Copy link
Author

I corrected the last example, which scalac compiles together but not separately.

dotc also compiles them separately.

@som-snytt som-snytt added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Jan 28, 2020
@som-snytt
Copy link
Author

Per odersky no sane person does that.

These are REPL use cases, and no one uses the REPL that way anyway.

@SethTisue SethTisue removed this from the Backlog milestone Jan 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) package objects
Projects
None yet
Development

No branches or pull requests

3 participants