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 = " + x)
  println(adder2(10))
  println("x = " + x)
}

解释

Explanation

f(a) _ and f(a)(_) have different meanings, governed by different sections of the Scala language specification.

f(a)(_) is the placeholder syntax for anonymous functions, described in SLS §6.23. Evaluation is deferred.

f(a) _ is eta expansion, described in SLS §6.26.5. Arguments given are evaluated eagerly; only the method call itself is deferred.

In this example, the eta expansion in

val adder2 = add(counter) _

causes eager evaluation, as described: as soon as that line is run, Scala actually evaluates counter and binds 1, the result, to the counter argument for adder2. Hence x is 1 when first printed.

adder1, on the other hand, is an anonymous function, so its counter argument is only bound when adder1 is evaluated. Since x is already 1 at this point, counter will be 2 and the evaluation of adder1 prints 12. The value of x is now 2, as indicated by the program‘s output.

时间: 2024-08-07 08:19:37

Scala 趣题 20的相关文章

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