I graduated in 1990 in Electrical Engineering and since then I have been in university, doing research in the field of DSP. To me programming is more a tool than a job.
In Ada you can use switch .. case (actually it is case .. when like in Ruby) only with discrete types: integers and enumerations. The nice part of the case in Ada is that you are obligated to specify all the cases. It can seem like a burden, but it saved me from many bugs that would had happened because I forgot to update a case after adding a new item to an enumeration type.
Of course there is the "default" case (when other), but I try to avoid it since it "disables" the safety check that all the cases are specified.
What about when you do a case with an integer? You cannot, of course, specify all the integers... Well, in that case you can use the when other (it is legit, after all), but often is better to define a new integer type with explicit bounds.
typeGrade='A+'|'A'|'B+'|'B'|'C'|'D'|'F';functionstudentFinalResult(grade:Grade):string{switch(grade){case'A+':return'Nailed It! 🥳';case'A':case'B+':case'B':return'Passed 💃';case'C':return'Barely Survived 😌';case'D':case'F':return'Failed 😢';default:const_exhaustiveCheck:never=grade;// "Type 'string' is not assignable to type 'never'".return_exhaustiveCheck;// if one of the union type values is missing}}console.log(studentFinalResult('A+'));// "Nailed It! 🥳"
A side note from a different language...
In Ada you can use
switch .. case
(actually it iscase .. when
like in Ruby) only with discrete types: integers and enumerations. The nice part of thecase
in Ada is that you are obligated to specify all the cases. It can seem like a burden, but it saved me from many bugs that would had happened because I forgot to update acase
after adding a new item to an enumeration type.Of course there is the "default" case (
when other
), but I try to avoid it since it "disables" the safety check that all the cases are specified.What about when you do a
case
with an integer? You cannot, of course, specify all the integers... Well, in that case you can use thewhen other
(it is legit, after all), but often is better to define a new integer type with explicit bounds.TypeScript supports exhaustiveness checking on union types.
playground