0

It seems like if a case class has both enums and options, I cannot instantiate it from Java.

Consider the following in 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:

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! Any idea how I can instantiate EnumOption in Java???

Mahdi
  • 1,778
  • 1
  • 21
  • 35
  • Is this a bug in scala compiler? (or maybe just a bug in Eclipse??) – Mahdi Dec 15 '15 at 10:43
  • The error is that no constructor with that signature can be found. I will add the exact message to the question. – Mahdi Dec 15 '15 at 12:54
  • I see no error with IntelliJ. What versions of Java, Scala are you using? – tuxdna Dec 15 '15 at 12:57
  • I am getting crazy. Now I see no error either! So it was just an eclipse bug, and it is not reproducible! :( So much for the time I wasted on fixing it! – Mahdi Dec 15 '15 at 13:01

1 Answers1

0

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 Options around and the combinations would grow radically. But for my case (the real application) I had three combinations. Of course, I hope there will be a better solution.

Mahdi
  • 1,778
  • 1
  • 21
  • 35