// #PS:90 // psn > a30 > a80 scala> // Use Enumeration to create a group of Constant just like scala> // in any other language scala> scala> // Example 1 scala> object WeekDay extends Enumeration { | val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value | } defined module WeekDay scala> import WeekDay._ import WeekDay._ scala> def isWorkingDay(d: Value) = ! (d == Sat || d == Sun) isWorkingDay: (d: WeekDay.Value)Boolean scala> WeekDay.values filter isWorkingDay foreach println Mon Tue Wed Thu Fri scala> scala> //Example 2 scala> object WeekDay extends Enumeration { | //Give alias for 'Value' type as 'WeekDay' | type WeekDay = Value | //In this line 'Value' is a method, whereas the 'Value' | //used in the previous line represents a Type | //val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value("Day") | val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value | } defined module WeekDay scala> import WeekDay._ import WeekDay._ scala> def isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun) isWorkingDay: (d: WeekDay.WeekDay)Boolean scala> WeekDay.values filter isWorkingDay foreach{x => | println(x, x.id) | } (Mon,0) (Tue,1) (Wed,2) (Thu,3) (Fri,4)
Enumeration : Example
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment