在 Scala 中初始化一个 var

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2754301/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 02:07:31  来源:igfitidea点击:

initialise a var in scala

scala

提问by user unknown

I have a class where I like to initialize my var by reading a configfile, which produces intermediate objects/vals, which I would like to group and hide in a method. Here is the bare minimum of the problem - I call the ctor with a param i, in reality a File to parse, and the init-method generates the String s, in reality more complicated than here, with a lot of intermediate objects being created:

我有一个类,我喜欢通过读取配置文件来初始化我的 var,该文件生成中间对象/val,我想将它们分组并隐藏在方法中。这是问题的最低限度 - 我用参数 i 调用 ctor,实际上是要解析的文件,而 init 方法生成 String s,实际上比这里更复杂,创建了许多中间对象:

class Foo (val i: Int) {

    var s : String;

    def init () {
        s = "" + i 
    }

    init ()
}

This will produce the error: class Foo needs to be abstract, since variable s is not defined. In this example it is easy to solve by setting the String to "": var s = "";, but in reality the object is more complex than String, without an apropriate Null-implementation.

这将产生错误:class Foo needs to be abstract, since variable s is not defined。在这个例子中,通过将 String 设置为 "": 很容易解决var s = "";,但实际上对象比 String 更复杂,没有适当的 Null 实现。

I know, I can use an Option, which works for more complicated things than String too:

我知道,我可以使用 Option,它也适用于比 String 更复杂的事情:

var s : Option [String] = None

def init () {
    s = Some ("" + i) 
}

or I can dispense with my methodcall. Using an Option will force me to write Some over and over again, without much benefit, since there is no need for a None else than to initialize it that way I thought I could.

或者我可以省去我的方法调用。使用 Option 将迫使我一遍又一遍地编写 Some ,没有太大好处,因为除了以我认为可以的方式初始化它之外,不需要 None 。

Is there another way to achieve my goal?

还有其他方法可以实现我的目标吗?

回答by sepp2k

var s : Whatever = _will initialize s to the default value for Whatever (null for reference types, 0 for numbers, false for bools etc.)

var s : Whatever = _将 s 初始化为 What 的默认值(引用类型为 null,数字为 0,bool 为 false 等)

回答by missingfaktor

Instead of creating separate methods for initialization, you should perform the initialization using the following way :

您应该使用以下方式执行初始化,而不是创建单独的初始化方法:

class Foo(val i: Int) {
  var s: String = {
    var s0 = " "
    s0 += i
    // do some more stuff with s0
    s0
  }

  var dashedDate = {
    val dashed = new SimpleDateFormat("yy-MM-dd")
    dashed.format(updated)
  }

  // Initializing more than one field:
  var (x, y, z) = {
    var x0, y0, z0 = 0
    // some calculations
    (x0, y0, z0)
  }
}

回答by egervari

Honestly, why are you using var? Why not just do:

老实说,你为什么使用var?为什么不这样做:

val rootObject = readFile(filename)

This would make the most sense to me.

这对我来说是最有意义的。