4 ms·
> Do most (or any) languages in which people take this approach actually enforce handling of all cases? I'm not familiar with most languages, but I can state t
by anw 6y ago
> Do most (or any) languages in which people take this approach actually enforce handling of all cases?
I'm not familiar with most languages, but I can state that Rust requires matching sum types to cover all possible variants. For example:
enum Animal {
Dog,
Cat,
Pigeon
}
let pet = Animal::Dog;
let sound = match pet {
Animal::Dog => String::from("ouaf"),
Animal::Cat => String::from("miaou")
};
This would not compile, because you've forgotten to cover the pigeon. You would need to either add one of the two lines below:
Animal::Pigeon => String::from("rou rou")
or
_ => String::from("eeeeeeeeeeeei")
The last line is a catch all, which would cover anything not checked for explicitly.