File tree Expand file tree Collapse file tree 2 files changed +33
-29
lines changed
docs/docs/reference/enums Expand file tree Collapse file tree 2 files changed +33
-29
lines changed Original file line number Diff line number Diff line change @@ -88,35 +88,6 @@ object Planet {
8888 println(s " Your weight on $p is ${p.surfaceWeight(mass)}" )
8989 }
9090}
91- ```
92- ### A specific use case of enum
93-
94- Two enums can be combined into a different type as shown below:
95-
96- ``` scala
97-
98- trait Animal
99- object Animal {
100- def enumValues : Iterable [Animal ] = Dog .enumValues ++ Cat .enumValues
101- def enumValueNamed = Dog .enumValueNamed ++ Cat .enumValueNamed
102- }
103-
104- enum Dog extends Animal {
105- case GermanShepherd , Labrador , Boxer
106- }
107-
108- enum Cat extends Animal {
109- case PersianCat , Ragdoll , ScottishFold
110- }
111-
112- object Main {
113- def main (args : Array [String ]): Unit = {
114- val values : Iterable [Animal ] = Animal .enumValues
115- val boxer_dog : Animal = Animal .enumValueNamed(" Boxer" )
116- val ragdoll_cat : Animal = Animal .enumValueNamed(" Ragdoll" )
117- }
118- }
119-
12091```
12192
12293### Implementation
Original file line number Diff line number Diff line change 1+ trait Animal
2+ object Animal {
3+ def enumValues : Iterable [Animal ] = Dog .enumValues ++ Cat .enumValues
4+ def enumValueNamed = Dog .enumValueNamed ++ Cat .enumValueNamed
5+ }
6+
7+ enum Dog extends Animal {
8+ case GermanShepherd , Labrador , Boxer
9+ }
10+
11+ enum Cat extends Animal {
12+ case PersianCat , Ragdoll , ScottishFold
13+ }
14+
15+ object Test {
16+ import Dog ._
17+ import Cat ._
18+
19+ def main (args : Array [String ]): Unit = {
20+ val values = List (
21+ GermanShepherd ,
22+ Labrador ,
23+ Boxer ,
24+ PersianCat ,
25+ Ragdoll ,
26+ ScottishFold
27+ )
28+
29+ assert(Animal .enumValues == values)
30+ assert(Animal .enumValueNamed(" Boxer" ) == Boxer )
31+ assert(Animal .enumValueNamed(" Ragdoll" ) == Ragdoll )
32+ }
33+ }
You can’t perform that action at this time.
0 commit comments