Scala 趣题 19 包名和对象名等不能重

package pzs

object base {
  class X { val context = "object base" }
}

package base {
  class Y { val context = "package base" }
}

object Main extends App {
  println((new base.X).context)
  println((new base.Y).context)
}

Explanation

According to the SLS (§9.2) it is illegal to have a package with the same fully qualified name as a module or a class. For example, on the following compilation unit

package com.scalapuzzlers

object base { class X }

package base { class X }

the compiler will report an error:

X.scala:3: error: base is already defined as object base

package base { class X }

^

one error found

Top-level definitions outside a packaging however are assumed to be injected into a special empty package. That package can neither be named nor imported, but the members of the empty package are visible to each other without qualification.

The example above defines the package base and the top-level object <empty>.base, where the latter is visible without qualification, i.e. the object shadows the package. Thus the first println statement would compile fine and print object base. The access to base.Y in the second println statement leads to the compiler error type Y is not a member of object base.

Class Y in package base can be accessed with a full qualified identifier:

println((new _root_.base.Y).context)

Members of the empty package are however only visible from within the empty package, i.e. only from other top-level classes. In all other packages, top-level objects and classes are not accessible. Real projects will thus never use top-level classes.

时间: 2024-10-14 12:54:22

Scala 趣题 19 包名和对象名等不能重的相关文章

Scala 趣题 22 函数重写,位置参数,参数名

  class C {     def sum(x: Int = 1, y: Int = 2): Int = x + y   }   class D extends C {     override def sum(y: Int = 3, x: Int = 4): Int = super.sum(x, y)   }   val d: D = new D   val c: C = d   c.sum(x = 0)   d.sum(x = 0) Explanation Scala uses the

Scala 趣题 17 隐式参数和偏函数

输出是多少?   implicit val z1 = 2   def addTo(n: Int) = {     def add(x: Int)(y: Int)(implicit z: Int) = x + y + z     add(n) _   }      val addTo1 = addTo(1)  println( addTo1(2)) 原题为 implicit val z1 = 2 def addTo(n: Int) = {    def add(x: Int)(y: Int)(im

Scala 趣题 4

A {   foo: Int   = (+ foo + + ) } B A {   : Int = (+ + + ) } C B {   = (+ + + ) } C Explanation Notice that bar is a val that is overridden in C. The Scala compiler will only initialize vals once, so since bar will be initialized in C it is not initi

Scala 趣题 5 The missing list

回忆下Scala的集合框架, 可能有助于理解下面的题目 两个sum的结果一样吗? sumSizes(collections: [[_]]): Int =  sumSizes(((, ), (, ))) sumSizes(((, ), (, ))) Explanation Even though collections.map would appear to map an iterable to another "nice" iterable, since the Collections

Scala 趣题 21 x.type ?

  class X   val x = new X   val y = x: x.type   object Overload {     def foo(arg: Any) = 1     def foo(arg: x.type) = 2   }   println(Overload.foo(x))   println(Overload.foo(y: y.type)) Explanation If the method Overload.foo(x) is invoked, then over

Scala 趣题 16 return 语句

value = ?   def value: Int = {     def one(x: Int): Int = { return x; 1}     val two = (x: Int) => { return x; 2 }     1 + one(2) + two(3)   }      println(value) 解释 有点复杂 Explanation Scala does not complain about unreachable code, therefore the code

Scala 趣题 14 当自己调自己的时候

真是无奇不有, 下面的两个println语句哪个抛空指针,那个输出8 ?   val s1: String = s1   println(s1.length)   val s2: String = s2 + s2   println(s2.length) 解释: String类型默认被初始化成nulll null引用出现在字符串的位置按照语言规范默认被转化成字符串"null" Explanation The definitions of the values s1 and s2 are

Scala 趣题 18 偏函数对默认参数的影响

下面程序结果会是什么? package pzs object Pz018 extends App {   def invert(v3: Int)(v2: Int = 2, v1: Int = 1) {     println(v1 + ", " + v2 + ", " + v3);   }   def invert3 = invert(3) _   invert3(v1 = 2)   invert3(v1 = 2, v2 = 1) } 解释 eta展开,丢失了默认参

Scala 趣题 20

package pzs object Pz020 extends App {   var x = 0   def counter = { x += 1; x }   def add(a: Int)(b: Int) = a + b   val adder1 = add(counter)(_)   val adder2 = add(counter) _   println("x = " + x)   println(adder1(10))   println("x = "