[英]Instantiate a case class with Options and Scala Enumeration in Java
It seems like if a case class has both enums and options, I cannot instantiate it from Java. 似乎如果case类同时具有枚举和选项,则无法从Java实例化它。
Consider the following in Scala: 在Scala中考虑以下内容:
object WeekDay extends Enumeration {
type WeekDay = Value
val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
case class EnumOption(e: WeekDay.Value, s: Option[String])
case class EnumOnly (e: WeekDay.Value, s: String)
case class OptionOnly(e: Int, s: Option[String])
And the following in Java to use them: 并在Java中使用以下代码:
scala.Enumeration.Value monday = WeekDay.Mon();
EnumOption a = new EnumOption(monday, Option.apply("12"));
EnumOnly b = new EnumOnly(monday, "12");
OptionOnly c = new OptionOnly(12, Option.apply("12"));
I get an error (at least Eclipse shows me an error) on instantiating a
but b
and c
work just fine! 我在实例化
a
遇到错误(至少Eclipse向我显示了一个错误),但是b
和c
正常工作! Any idea how I can instantiate EnumOption
in Java??? 任何想法如何在Java中实例化
EnumOption
???
EDIT: Now the same code gives me no error. 编辑:现在相同的代码给我没有错误。 So it was an eclipse bug, and it is not reproducible!
因此,这是一个月食错误,并且不可复制!
Disclaimer: This is only a workaround that I have currently opted for. 免责声明:这只是我当前选择的解决方法。
case class EnumOption(e: WeekDay.Value, s: Option[String])
object EnumOption {
def optionAvailable(e: WeekDay.Value, s: String) = new EnumOption(e, Some(s))
def notAvailable(e: WeekDay.Value) = new EnumOption(e, None)
}
and then use either of the two methods above. 然后使用以上两种方法中的任何一种。
Clearly this is not a viable solution if there are many Option
s around and the combinations would grow radically. 显然,如果周围有很多
Option
,并且组合会急剧增长,那么这不是可行的解决方案。 But for my case (the real application) I had three combinations. 但是对于我的情况(实际应用),我有三种组合。 Of course, I hope there will be a better solution.
当然,我希望会有更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.