Reputation: 1467
Let's assume we have a map indexed by enum values like this
sealed trait A
case object B extends A
case object C extends A
type SafeMap = Map[A, String]
Would it be possible to somehow enforce the exhaustiveness of such map? Maybe with dependent types? If not in Scala than Idris is interesting as well.
To clarify, I would like to have a generic constraint for any ADT that check that Map has an entry defined for each variant of ADT.
Upvotes: 1
Views: 230
Reputation: 1016
well a Map[A,String]
for an ADT A
is pretty much a function A => String
You can do
def foo(a:A):String = a match {
case ...
}
with the fatal-warning flag for the compiler (else non-exhaustive patternmatches are only warnings) this should get you pretty far.
PS: you should probably make your case classes/object final
Upvotes: 4