Reputation: 2280
In scala what is the difference in defining Enumeration as below
object TargetRelation extends Enumeration {
val Organizations = "organizations"
val Individuals = "individuals"
val Things = "things"
}
object TargetRelation extends Enumeration {
type TargetRelation = Value
val Organizations = Value("organizations")
val Individuals = Value("individuals")
val Things = Value("things")
}
My Questions
Here is the code snippet
Note : target.resourceType is a String
target.resourceType match {
case TargetRelationType.Individuals => {}
case TargetRelationType.Organizationse => {}
}
Upvotes: 1
Views: 63
Reputation: 372
The first one is not an Enumeration. You could leave out extends Enumeration and achieve the same result. It is just a singleton with a few string constants
Upvotes: 1
Reputation: 27535
This one:
object TargetRelation extends Enumeration {
type TargetRelation = Value
val Organizations = Value("organizations")
val Individuals = Value("individuals")
val Things = Value("things")
}
will use build-in objects and types of Enumeration
trait to:
Value
Value
String
into Value
where TargetRelation.Value
is the actual enumeration type (which is why people sometime alias it to companion object name and import directly the alias).
This one:
object TargetRelation extends Enumeration {
val Organizations = "organizations"
val Individuals = "individuals"
val Things = "things"
}
doesn't use Value
anywhere so it is virtually the same as this one:
object TargetRelation {
val Organizations = "organizations"
val Individuals = "individuals"
val Things = "things"
}
meaning - this is just namespace for constant String
s.
However, from type safety point of view both are hardly useful as neither can be checked for exhaustiveness.
targetRelation match {
case TargetRelation.Organizations => ...
// no warning or error about which cases are missing
}
If you are in need of something that work like Enum
: name parsing, automating name and ordering generation, exhaustiveness checking - both approach are deprecated by community which adopted sealed trait
s with case object
s (and enumeratum) instead:
// this ensured that matching is exhaustive
sealed trait TargetRelation
object TargetRelation {
case object Organizations extends TargetRelation
case object Individuals extends TargetRelation
case object Things extends TargetRelation
}
// like above but with tons of utilities
import enumeratum._
sealed trait TargetRelation extends EnumEntry
object TargetRelation extends Enum[TargetRelation] {
case object Organizations extends TargetRelation
case object Individuals extends TargetRelation
case object Things extends TargetRelation
val values = findValues
}
With that you can use TargetRelation.withName("organizations")
during parsing phase (there is a support for Circe and many other libraries) and compare parsed value against case objects of a sealed hierarchy which is type safe, faster etc.
Upvotes: 3