Scala主要功能的默认参数? [英] Default arguments to Scala's main function?

查看:105
本文介绍了Scala主要功能的默认参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试过如何使用和不使用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名称;而不是另一个varval.另外,范围必须在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆