14
The Swift language presents a very interesting and very intuitive way to work with intervals using switch-case, with "partial matching" techniques, "Pattern-matching" etc, look at these examples:
let age = 33
switch(age){
case let x where x >= 0 && x <= 2:
print("Infant")
case let x where x >= 3 && x <= 12:
print("Child")
case let x where x >= 13 && x <= 19:
print("Teenager")
case let x where x >= 20 && x <= 39:
print("Adult")
case let x where x >= 40 && x <= 60:
print("Middle aged")
case let x where x >= 61:
print("Elderly")
default:
print("Invalid")
}
Another very interesting example:
let string = "Dog"
switch(string){
case "Cat","Dog":
print("Domestic animal")
case "Lion","Leopard","Pantera":
print("Never touch me")
default:
print("By the way, don't touch... :-)")
}
Are there other languages that enable these features? In Java I’ve never been able to make a switch case like this. I’ve also never been curious to know if it would work :-) I’ve always used a different logic approach.
Editing: Adding an incredible example of "partial matching":
let coordinates: (x: Int, y: Int, z: Int) = (3, 0, 0)
switch (coordinates) {
case (0, 0, 0): // 1
print("Origin")
case (_, 0, 0): // 2
print("On the x-axis.")
case (0, _, 0): // 3
print("On the y-axis.")
case (0, 0, _): // 4
print("On the z-axis.")
default: // 5
print("Somewhere in space")
}
In the above case, the program will analyze by default, since it is a point in X and the rest of the axes at zero. "On the X Axis". In Java you would not use a switch case and simply a getx of the object no matter the rest, everything with Else if, would write less and have the same result. But I thought it was different the way Swift had this freedom.
It seems to me that VB also has a similar technique.
Hugs to all.
In the second case, in Java would look like this:
case "Cat": case "Dog": {lógica} break;
.– Renan Gomes
@True! In the second case is possible even, you’re right! In the first case it’s a bit interesting right! I can complement with other examples as well. I added an example of "partial matching"
– Mateus
To my knowledge, this type of matching is first emerged in functional programming languages (ML, Haskell, Ocaml, and the like). It is especially useful when combined with algebraic data types (tagged).
– hugomg
@Matthew I know the question is long overdue but the use of the Let Where case is totally unnecessary if you don’t use the variable x in your cases. In this case you should use ranges in your cases and in the latter case if it is Swift 4 you can use partial range operato if you do not want to put an age limit. For example
switch age {
case 0...2:
 print("Infant")
case 3...12:
 print("Child")
case 13...19:
 print("Teenager")
case 20...39:
 print("Adult")
case 40...60:
 print("Middle aged")
case 61...:
 print("Elderly")
default:
 print("Invalid")
}
– Leo Dabus
Another thing none of the answers even addressed the use of keyword fallthrough. A good example is this answer that I put on the site in English https://stackoverflow.com/a/31782490/2303865
– Leo Dabus
Also missing show use of switch with enumerations in Swift where it is not necessary to add default case (about being exhaustive) Check out Nate Cook’s response to this https://stackoverflow.com/a/26686733/2303865
– Leo Dabus