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

Package import question.

1 reply
Petr Gladkikh
Joined: 2009-01-20,
User offline. Last seen 42 years 45 weeks ago.
Hello.

Scala language specification says that

Example 9.3.1 Consider the following program:
 package b {
    class B
 }
 package a.b {
    class A {
      val x = new _root_.b.B
    }
}
Here, the reference _root_.b.B refers to class B in the toplevel package b. If the
_root_ prefix had been omitted, the name b would instead resolve to the package
a.b, and, provided that package does not also contain a class B, a compiler-time
error would result.

To me it is counter intuitive that relative reference class b.B inside a.b.A class definition would be resolved to a.b.B not a.b.b.B
Moreover a class a.b.C can be resolved inside a.b.A class definition as b.C and just C. Everywhere else 'this' context is always "prefix-less" why is this redundancy?

To me it looks like some implementation detail that creeped out into specification. Are there any explanations why it is so?

To my taste it would be sensible to have some prefix that says "this package" without naming it explicitly.

--
Petr Gladkikh

Ricky Clarkson
Joined: 2008-12-19,
User offline. Last seen 3 years 2 weeks ago.
Re: Package import question.
Scala's packages are hierarchical.  Scala's import is relative.

package a.b defines a package a and a package b that is a child of..

package a.b defines a package a and a package b that is a child of a.  Thus looking for b.A will look in a.b, not in _root_.b.

I don't think this is implementation leaking to spec, but as it stands, it's not particularly useful.

If Scala's packages could be used as values (so they'd be little different to the object construct) then the relative import might have more use.  As it is, it just gets in the way.

It's been discussed before on the lists, but Martin wants to keep it how it is now rather than entertain improvement proposals.

2009/1/21 Petr Gladkikh <petrglad [at] gmail [dot] com>
Hello.

Scala language specification says that

Example 9.3.1 Consider the following program:
 package b {
    class B
 }
 package a.b {
    class A {
      val x = new _root_.b.B
    }
}
Here, the reference _root_.b.B refers to class B in the toplevel package b. If the
_root_ prefix had been omitted, the name b would instead resolve to the package
a.b, and, provided that package does not also contain a class B, a compiler-time
error would result.

To me it is counter intuitive that relative reference class b.B inside a.b.A class definition would be resolved to a.b.B not a.b.b.B
Moreover a class a.b.C can be resolved inside a.b.A class definition as b.C and just C. Everywhere else 'this' context is always "prefix-less" why is this redundancy?

To me it looks like some implementation detail that creeped out into specification. Are there any explanations why it is so?

To my taste it would be sensible to have some prefix that says "this package" without naming it explicitly.

--
Petr Gladkikh


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