Scala主要功能的默认参数? [英] Default arguments to Scala's main function?
问题描述
我尝试过如何使用和不使用new
关键字:
How do I get this to work, I've tried with and without new
keyword:
object Main extends App {
override def main (args : Array[String] = new Array("default argument")) {
println("args(0) = " + args(0))
}
}
http://ideone.com/5AyTxy (我尝试了其他 2] 方法,但我认为它的val
引起了问题)
http://ideone.com/5AyTxy (I have tried other [1, 2] methods but I think that its val
is causing issues)
文档: http://docs. scala-lang.org/sips/completed/named-and-default-arguments.html#default_arguments
PS:我应该确认要将此名称绑定到args
名称;而不是另一个var
或val
.另外,范围必须在main
中是全局的.
PS: I should confirm that I want this to be bound to the args
name; rather than to another var
or val
. Additionally the scope needs to be global within main
.
推荐答案
要在这里满足您的意图(正如您对@AlexIv的答案的最后评论所阐明的那样)-保留args
的字段名称,同时允许使用默认值字段-您可以使用对super.
前缀的原始值的引用来覆盖该字段:
To satisfy your intent here (as clarified by your last comment to @AlexIv's answer) - keeping the field name of args
while allowing for a default value for the field - you can override the field, using a reference to the original value prefixed by super.
:
object Main extends App {
override val args: Array[String] = if (super.args.isEmpty) Array("default argument") else super.args
println("args(0) = " + args(0))
}
这篇关于Scala主要功能的默认参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!