I am new to Scala, so please be kind. You must excuse me if I miss something obvious.
I am trying to create an enum like structure to represent the days of the week in Scala. I want a method that takes a string, which can be a number from 1 to 7, the full name of the day or an abbreviation of three letters with any capitalization and returns the correct day. Ideally, I want to get the correct day by simply writing DayOfWeek(_) , which, as I understand it, means that this method should be apply . Individual values ββmust also be subclassed (or mixed) by the CalendarField attribute, which currently does not define methods or members.
This is my current attempt:
object DayOfWeek extends Enumeration with CalendarField { val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = Value def apply(v:String) = { v.toUpperCase match { case Int(x) => x match { case 1 => Sunday case 2 => Monday case 3 => Tuesday case 4 => Wednesday case 5 => Thursday case 6 => Friday case 7 => Saturday case _ => throw new IllegalArgumentException("Invalid value for day of week: " + v) } case "SUN" | "SUNDAY" => Sunday case "MON" | "MONDAY" => Monday case "TUE" | "TUEDAY" => Tuesday case "WED" | "WEDNESDAY" => Wednesday case "THU" | "THURSDAY" => Thursday case "FRI" | "FRIDAY" => Friday case "SAT" | "SATURDAY" => Saturday case _ => throw new IllegalArgumentException("Invalid value for day of week: " + v) } } } object Int { def unapply(s : String) : Option[Int] = try { Some(s.toInt) } catch { case _ : java.lang.NumberFormatException => None } }
This does not work at several points:
- Individual values ββare not subclasses of
CalendarField - they are simply Value s. - I defined an
apply method, but DayOfWeek(_) does not work - my guess is (undefined) because it expects a constructor call, and DayOfWeek is an object, not a class? Or is it because Enumeration (s: String) is already in use? Is there any way around this?
Any suggestions on how to get around these problems or new solutions are welcome. Thanks.
source share