I have two different enumeration types, one of which is for the standard directions North, South, East, West
, while the other includes those as well as Northeast, Northwest, Southeast, Southwest
. Is there any way I can define a relationship between these two enumerations to avoid duplicate and confusing code, or must they remain separate because they have different possible values (despite one being a subset of the other)?
Asked
Active
Viewed 370 times
1

Kvass
- 8,294
- 12
- 65
- 108
1 Answers
2
If you use case objects instead of enumerations (which I think would be considered more idiomatic anyway) then you could do it like this:
sealed trait Directions8
sealed trait Directions4 extends Directions8
case object North extends Directions4
case object South extends Directions4
case object East extends Directions4
case object West extends Directions4
case object Northeast extends Directions8
case object Northwest extends Directions8
case object Southeast extends Directions8
case object Southwest extends Directions8

DaoWen
- 32,589
- 6
- 74
- 101
-
From an idiomatic perspective, when (if ever) are enumerations considered appropriate? – Kvass May 27 '14 at 03:11
-
2There's another question that discusses that: [Case classes vs Enumerations in Scala](http://stackoverflow.com/questions/1898932) – DaoWen May 27 '14 at 03:14