Scala 中带有可选字段的案例类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11982474/
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 04:26:55  来源:igfitidea点击:

Case Classes with optional fields in Scala

scala

提问by jeypijeypi

For example, I have this case class:

例如,我有这个案例类:

case class Student (firstName : String, lastName : String)

If I use this case class, is it possible that supplying data to the fields inside the case class are optional? For example, I'll do this:

如果我使用这个案例类,是否有可能向案例类中的字段提供数据是可选的?例如,我会这样做:

val student = new Student(firstName = "Foo")

Thanks!

谢谢!

回答by om-nom-nom

You were close:

你很接近:

case class Student (firstName : String = "John", lastName : String = "Doe")

val student = Student(firstName = "Foo")

Another possibility is partially applied function:

另一种可能性是部分应用函数:

case class Student (firstName : String, lastName : String)

val someJohn = Student("John", _: String)
//someJohn: String => Student = <function1>

val johnDoe = someJohn("Doe")
//johnDoe: Student = Student(John,Doe)

And to be complete, you can create some default object and then change some field:

为了完整起见,您可以创建一些默认对象,然后更改一些字段:

val johnDeere = johnDoe.copy(lastName="Deere")
//johnDeer: Student = Student(John,Deere)

回答by tgr

If you just want to miss the second parameter without a default information, I suggest you to use an Option.

如果您只想错过没有默认信息的第二个参数,我建议您使用Option.

case class Student(firstName: String, lastName: Option[String] = None)

Now you might create instances this way:

现在您可以通过以下方式创建实例:

Student("Foo")
Student("Foo", None)            // equal to the one above
Student("Foo", Some("Bar"))     // neccesary to add a lastName

To make it usable as you wanted it, I will add an implicit:

为了让它可以随心所欲地使用,我将添加一个隐含的:

object Student {
  implicit def string2Option(s: String) = Some(s)
}

Now you are able to call it those ways:

现在你可以这样称呼它:

import Student._

Student("Foo")
Student("Foo", None)
Student("Foo", Some("Bar"))
Student("Foo", "Bar")

回答by akauppi

I would see two ways this is normally done.

我会看到通常这样做的两种方式。

1. default parameters

1.默认参数

case class Student (firstName : String, lastName : String = "")

Student("jeypijeypi")   # Student(jeypijeypi,)

2. alternative constructors

2. 替代构造函数

case class Student (firstName : String, lastName : String)

object Student {
    def apply(firstName: String) = new Student(firstName,"")
}

Student("jeypijeypi")   # Student(jeypijeypi,)

Which one is better depends slightly on the circumstances. The latter gives you more freedom: you can make any parameter(s) optional, or even change their order (not recommended). Default parameters need always to be at the end of the parameter list, I think. You can also combine these two ways.

哪个更好取决于具体情况。后者为您提供了更多的自由:您可以将任何参数设为可选,甚至更改它们的顺序(不推荐)。我认为,默认参数需要始终位于参数列表的末尾。您也可以将这两种方式结合起来。

Note: within the alternative constructors you need newto point the compiler to the actual constructor. Normally newis not used with case classes.

注意:在替代构造函数中,您需要new将编译器指向实际的构造函数。通常new不与案例类一起使用。