[英]Def vs. val, functional syntax not working in Scala.js?
To give you a minimal example: 给你一个最小的例子:
object Main extends JSApp
{
val someThing: String = determineSomething("test")
def main(): Unit =
{
println(someThing)
}
}
Now, two possibilities here: 现在,这里有两种可能性:
private def determineSomething(s: String): String = "succeeded"
If the project is executed like this, well, I get a log entry saying 如果项目是这样执行的,那么我会得到一条日志条目,内容为
succeeded
成功了
But when I use the more functional syntax: 但是,当我使用更具功能性的语法时:
private val determineSomething: (s: String) => "succeeded"
I get 我懂了
TypeError: this.determineSomething$1 is null
I am curious as to the why this happens as in the (JVM) repl, both ways work perfectly fine. 我很好奇为什么在(JVM)repl中会发生这种情况,两种方式都可以正常工作。
I think what you want is something like this: 我认为您想要的是这样的:
object Main extends JSApp {
private val determineSomething: String => String = (s: String) => "succeeded"
val someThing: String = determineSomething("test")
def main(): Unit = {
println(someThing)
}
}
The declaration of determineSomething
needs to come before the declaration of something
, otherwise the former will be uninitialized when the compiler attempts to initialize the latter. 声明
determineSomething
需要来申报前something
,否则当编译器尝试初始化后者,前者将被初始化。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.