构造函数的按名称参数
来自我的其他问题 有没有办法让构造函数按名称参数工作?我需要一种方法来提供一个在对象内按需/惰性/按名称执行的代码块,并且该代码块必须能够访问类方法,就好像代码块是类的一部分一样。
以下测试用例失败:
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
coming from my other question is there a way to get by-name-parameters for constructors working? I need a way to provide a code-block which is executed on-demand/lazy/by-name inside an object and this code-block must be able to access the class-methods as if the code-block were part of the class.
Following Testcase fails:
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
}
}
}
Output:
foo
method
pre-code
post-code
--------
pre-code
foo
post-code
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是因为当您创建这样的实例时:
.. 您实际上是在创建一个匿名类,就像在 java 中一样。
上面的代码与:
.. 相同,而按名称传递的正确语法是:
对于构造函数,不能像函数那样省略括号。
This is because when you're making an instance like this:
.. you're actually creating an anonymous class, like in java.
The above code is the same as:
.. while the correct syntax for passing by-name is:
You cant omit parentheses the same way for constructors as with functions.
认为这样做可能更容易:
Thought it is probably just easier to do this:
我不知道为什么,但似乎在创建类时使用 {} 或 () 会改变行为。使用以下类,
现在可以根据
需要以另一种方式定义。似乎在第一个表示法中,编译器将括号解释为要计算为
Unit
的块,而不是调用时计算为Unit
的匿名函数>。FWIW,使用 ({ ... }) 也可以正常工作。
I dont know why, but it appears that using {} or () when creating the class changes the behavior. Using the following class,
Now instead if defined another way,
as desired. It seems as if in the first notation, the compiler is interpreting the brackets as a block to be evaluated to
Unit
, instead of an anonymous function which, when called, evaluates toUnit
.FWIW, using ({ ... }) also works fine.