Scala 趣题 15

true or false ?

  class C
  val x1, x2 = new C
  val y1 @ y2 = new C
  println(x1 == x2)
  println(y1 == y2)

解释

val p1, p2 = new C 是 val p1 = new C; val p2 = new C的简写

val [email protected] = new C 是一个pattern binder

Explanation

According to the SLS (§4.1), a value definition val p1, . . . , pn : T = e is a shorthand for the sequence of value definitions val p1 : T = e; ...; val pn : T = e. This means that in our example the expression new C is evaluated twice and therefore x1 == x2 is false.

The second value definition is a pattern binder which is defined in SLS §8.1.3. A pattern binder [email protected] consists of a pattern variable x and a pattern p. In our case the pattern p is a variable pattern which matches any value and binds the variable name (in our case y2) to that value. The pattern binder binds that value also to the pattern variable y1. As the same value is bound to both variables y1 and y2, the expression y1 == y2 is true.

时间: 2024-10-10 15:21:54

Scala 趣题 15的相关文章

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 趣题 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 趣题 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 趣题 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 趣题 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 趣题 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 A