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

Scala-Reference: error about name bindings?

6 replies
fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.
Hello,

I'm trying to compile the code showned in chapter 2 of Scala Reference from May 11, 2011, pages 16 and 17, about import and bindings.

Unfortunatly, it seems to not be consistent with what scalac 2.8.1 and 2.9.0-1 implements, and so I believe I made some kind of mistake. Please, could somebody explains what I'm doing wrong ?


Here is the actual code I try to compile (copy/paste from the spec does not work really well, so perhaps it's just an error here):


=========
package P {
  object X { val x = 1; val y = 2 }
}
package Q {
  object X { val x = true; val y = "" }
}

package P {
  import Console._
  object A {
    println("L4: "+X)
    object B {
      import Q._
      println("L7: "+X)
      import X._
      println("L8: "+x)
      object C {
        val x = 3
        println("L12: "+x)
        { import Q.X._
        println("L14: "+x)
        import X.y
        println("L16: "+y)
        { val x = "abc"
          import P.X._
          println("L19: "+y)
          println("L20: "+x)
}}}}}}

=========

scalac 2.8.1 errors:
============
% ~/java/scala-2.8.1.final/bin/scalac TestNameSpace.scala
TestNameSpace.scala:16: error: reference to X is ambiguous;
it is both defined in package P and imported subsequently by
import Q._
      println("L7: "+X)
                     ^
TestNameSpace.scala:17: error: reference to X is ambiguous;
it is both defined in package P and imported subsequently by
import Q._
      import X._
             ^
TestNameSpace.scala:22: error: scala.Console.println("L12: ".+(C.this.x)) of type Unit does not take parameters
        { import Q.X._
        ^
three errors found

scala 2.9.0-1 errors:
============
% ~/java/scala-2.9.0.1/bin/scalac TestNameSpace.scala
TestNameSpace.scala:16: error: reference to X is ambiguous;
it is both defined in package P and imported subsequently by
import Q._
      println("L7: "+X)
                     ^
TestNameSpace.scala:17: error: reference to X is ambiguous;
it is both defined in package P and imported subsequently by
import Q._
      import X._
             ^
TestNameSpace.scala:22: error: Unit does not take parameters
        { import Q.X._
        ^
three errors found
========


Thanks,
-- 
Francois ARMAND
http://fanf42.blogspot.com
http://www.normation.com
fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.
Re: Scala-Reference: error about name bindings?
On 15/07/2011 12:04, Francois wrote:
4E2010C4 [dot] 6030007 [at] gmail [dot] com" type="cite"> Hello,

I'm trying to compile the code showned in chapter 2 of Scala Reference from May 11, 2011, pages 16 and 17, about import and bindings.

Unfortunatly, it seems to not be consistent with what scalac 2.8.1 and 2.9.0-1 implements, and so I believe I made some kind of mistake. Please, could somebody explains what I'm doing wrong ?


Nothing on that ?

Thanks,
-- 
Francois ARMAND
http://fanf42.blogspot.com
http://www.normation.com
Johannes Rudolph 2
Joined: 2010-02-12,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Scala-Reference: error about name bindings?

Three things:

1. There's probably something fishy in the example with semicolon
inference. You have to add a blank line between a line containing
`println` and the line starting a new block with `{`.

2. I think the first two package declarations are meant to be defined
in an extra compilation unit. That makes some errors go away.

3. Now there are still two errors remaining:

[error] reference to x is ambiguous;
[error] it is both defined in object C and imported subsequently by
[error] import Q.X._
[error] println("L14: " + x)
[error] ^
[error] reference to y is ambiguous;
[error] it is imported twice in the same scope by
[error] import P.X._
[error] and import X.y
[error] println("L19: " + y)

And these two are commented out in the reference because they are
meant to fail. In the first case because a lower precedence wildcard
import (Q.X._) tries to shadow a definition of an outer scope of
higher precedence (val x =). In the second case the wildcard import
(P.X._) tries to shadow an explicit import from the outer scope (X.y).

So nothing to see here but perhaps the problem with blocks beginning
directly after a `println` call. It would probably be useful to
include all code snippets from the reference in tests so things like
that can't happen.

On Wed, Jul 20, 2011 at 2:07 PM, Francois wrote:
> On 15/07/2011 12:04, Francois wrote:
>
> Hello,
>
> I'm trying to compile the code showned in chapter 2 of Scala Reference from
> May 11, 2011, pages 16 and 17, about import and bindings.
>
> Unfortunatly, it seems to not be consistent with what scalac 2.8.1 and
> 2.9.0-1 implements, and so I believe I made some kind of mistake. Please,
> could somebody explains what I'm doing wrong ?
>
>
> Nothing on that ?
>
> Thanks,
>
> --
> Francois ARMAND
> http://fanf42.blogspot.com
> http://www.normation.com
>

odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: Re: Scala-Reference: error about name bindings?
I have added the blank lines. Thanks for noting this, Johannes! -- Martin

On Wed, Jul 20, 2011 at 2:34 PM, Johannes Rudolph <johannes.rudolph@googlemail.com> wrote:
Three things:

1. There's probably something fishy in the example with semicolon
inference. You have to add a blank line between a line containing
`println` and the line starting a new block with `{`.

2. I think the first two package declarations are meant to be defined
in an extra compilation unit. That makes some errors go away.

3. Now there are still two errors remaining:

[error] reference to x is ambiguous;
[error] it is both defined in object C and imported subsequently by
[error] import Q.X._
[error]           println("L14: " + x)
[error]                             ^
[error] reference to y is ambiguous;
[error] it is imported twice in the same scope by
[error] import P.X._
[error] and import X.y
[error]             println("L19: " + y)

And these two are commented out in the reference because they are
meant to fail. In the first case because a lower precedence wildcard
import (Q.X._) tries to shadow a definition of an outer scope of
higher precedence (val x =). In the second case the wildcard import
(P.X._) tries to shadow an explicit import from the outer scope (X.y).

So nothing to see here but perhaps the problem with blocks beginning
directly after a `println` call. It would probably be useful to
include all code snippets from the reference in tests so things like
that can't happen.


On Wed, Jul 20, 2011 at 2:07 PM, Francois <fanf42@gmail.com> wrote:
> On 15/07/2011 12:04, Francois wrote:
>
> Hello,
>
> I'm trying to compile the code showned in chapter 2 of Scala Reference from
> May 11, 2011, pages 16 and 17, about import and bindings.
>
> Unfortunatly, it seems to not be consistent with what scalac 2.8.1 and
> 2.9.0-1 implements, and so I believe I made some kind of mistake. Please,
> could somebody explains what I'm doing wrong ?
>
>
> Nothing on that ?
>
> Thanks,
>
> --
> Francois ARMAND
> http://fanf42.blogspot.com
> http://www.normation.com
>



--
Johannes

-----------------------------------------------
Johannes Rudolph
http://virtual-void.net



--
Martin Odersky
Prof., EPFL and Chairman, Typesafe
PSED, 1015 Lausanne, Switzerland
Tel. EPFL: +41 21 693 6863
Tel. Typesafe: +41 21 691 4967

fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.
Re: Re: Scala-Reference: error about name bindings?

On 20/07/2011 14:34, Johannes Rudolph wrote:
> Three things:
>
> 1. There's probably something fishy in the example with semicolon
> inference. You have to add a blank line between a line containing
> `println` and the line starting a new block with `{`.

Ah, thanks ! I completly missed that.

fanf
Joined: 2009-03-17,
User offline. Last seen 2 years 30 weeks ago.
Re: Re: Scala-Reference: error about name bindings?

On 20/07/2011 14:34, Johannes Rudolph wrote:
> Three things:
>
> 1. There's probably something fishy in the example with semicolon
> inference. You have to add a blank line between a line containing
> `println` and the line starting a new block with `{`.

Oh, I totally missed that ! Thank for the pointer.

> 2. I think the first two package declarations are meant to be defined
> in an extra compilation unit. That makes some errors go away.
>

OK, it wasn't clear for me... Why is that so ?

> 3. Now there are still two errors remaining:

As you say, these one are expected.

So thanks for the information !

Johannes Rudolph 2
Joined: 2010-02-12,
User offline. Last seen 42 years 45 weeks ago.
Re: Re: Scala-Reference: error about name bindings?

On Wed, Jul 20, 2011 at 3:01 PM, Francois wrote:
>> 2. I think the first two package declarations are meant to be defined
>> in an extra compilation unit. That makes some errors go away.
>
> OK, it wasn't clear for me... Why is that so ?

I think, because names from the same package defined in other
compilation units have lowest precedence (see item 4 in the list of
precedences) and therefore can be shadowed in inner scopes.

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