3

来自我的另一个问题 有没有办法让构造函数工作的名称参数?我需要一种方法来提供一个在对象内按需/延迟/按名称执行的代码块,并且该代码块必须能够访问类方法,就好像该代码块是该类的一部分一样.

以下测试用例失败:

package test

class ByNameCons(code: => Unit) {

    def exec() = {
        println("pre-code")
        code
        println("post-code")
    }

    def meth() = println("method")

    def exec2(code2: => Unit) = {
        println("pre-code")
        code2
        println("post-code")
    }
}


object ByNameCons {

    def main(args: Array[String]): Unit = {
        val tst = new ByNameCons {
            println("foo")
            meth() // knows meth() as code is part of ByNameCons
        }
        tst.exec() // ByName fails (executed right as constructor)


        println("--------")


        tst.exec2 { // ByName works
            println("foo")
            //meth() // does not know meth() as code is NOT part of ByNameCons
        }       
    }
}

输出:

foo
method
pre-code
post-code
--------
pre-code
foo
post-code
4

3 回答 3

6

This is because when you're making an instance like this:

val tst = new ByNameCons {
  ...
}

.. you're actually creating an anonymous class, like in java. The above code is the same as:

val tst = new ByNameCons() { ... }

.. while the correct syntax for passing by-name is:

val tst = new ByNameCons( { ... } )

You cant omit parentheses the same way for constructors as with functions.

于 2011-07-26T20:33:46.230 回答
3
val tst = new ByNameCons( {
   println("foo")  
} )

认为这样做可能更容易:

object ByNameCons {
  def apply(code: => Unit) = new ByNameCons(code)
}

val tst = ByNameCons { // no "new" here -- can't mix traits either
  println("foo")
}
于 2010-04-15T17:15:08.597 回答
1

我不知道为什么,但似乎在创建类时使用 {} 或 () 会改变行为。使用以下类,

class Coder(code: => Unit) {
  def exec = { 
    println("before")
    code
    println("after")}
  }
}

scala> val brackets = new Coder {println("testing")}
testing
brackets: Coder = $anon$1@1af7a03
scala> brackets exec
before
after

现在,如果以另一种方式定义,

scala> val parens = new Coder(println("testing"))
parens: Coder = Coder@dca3ed
scala> parens exec
before
testing
after

如预期的。似乎在第一种表示法中,编译器将括号解释为要评估为 的块Unit,而不是一个匿名函数,该函数在调用时评估为Unit

FWIW,使用 ({ ... }) 也可以正常工作。

于 2010-04-15T17:29:28.043 回答