|
|
|
|
|
by tel
4257 days ago
|
|
This happens absolutely all the time. For instance, Haskell does it in almost exactly these terms. If you have a type like data Drums = Ba | Dum | Tish
and you have a function that consumes drums play :: Drums -> String
play Ba = "Ba!"
play Dum = "Dum!"
you've left out the Tish. Possibly because you don't have a cymbal. You know this, but the compiler doesn't. This is considered to be an incomplete pattern match and would raise some eyebrows.But often it just means there's an invariant which you know and the type system does not. In Haskell -Wall will stop you dead here, but you can relax it to say "ehhhh, I know what I'm doing". More generally, you can also write play :: Drums -> String
play Ba = "Ba!"
play Dum = "Dum!"
play Tish = error "Impossible! I have no cymbal"
This 'error' is a misleadingly named function. It's more like 'assert' and indicates that this is a completely impossible program state (much like running into an unbalanced tree deep inside a library function which knows that tree balance is maintained). It's outside of the reach of the programmer to fix. It's not an exception, it's an assurance to the compiler that even though there's something missing in the type safety things will be OK. |
|