我有一个关于这两个函数之间区别的问题:
def getFunction(checkpointPath: String,
sparkConf: SparkConf,
creatingFunc: () => StreamingContext): StreamingContext = {
function body
}
def getFunction(checkpointPath: String,
sparkConf: SparkConf,
creatingFunc: => StreamingContext): StreamingContext = {
function body
}
所以 called by name 参数是相同的:
creatingFunc: => StreamingContext
和
creatingFunc: () => StreamingContext
还是不?
最佳答案
两者不一样。第一种情况指定方法参数是按名称调用
creatingFunc: => StreamingContext
而第二种情况指定按值传递方法参数,其中参数恰好是 () => StreamingContex
类型的函数
creatingFunc: () => StreamingContext
例如,考虑以下两种方法
def foo(arg: () => String) = ""
def foo(arg: => String) = ""
然后
foo(() => "") // resolves to call first foo
foo("") // resolves to call second foo
关于scala - 在 Scala 函数中按名称参数调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56428611/