I recently read Norms blog post at https://www.great-white-software.com/blog/ titled “Decide by not deciding” and it made me think about things that the ENUM in Swift can do that Xojo cannot (things that would be a great enhancement to Xojo)
In Xojo Enums are limited to INTEGER datatype (and while Swift is not, I am not going to go there in this post). But Xojo is also limited to a list of enum values… period.
So at the most primitive level, an Enum in both Xojo and Swift might look like this
enum vehicles
transport = 0
car = 1
mini = 2
impala = 3
and similarly in Swift
enum vehicle : Int {
transport = 0
car = 1
mini = 2
impala = 3
}
But getting back to Norm’s blog… and the following is just an illustration, not to say it is better, or worse, just to show what Swift Enums CAN DO
enum vehicle : Int {
transport = 0
car = 1
mini = 2
impala = 3
var weight : Int {
switch self {
case .transport : return 30000
case .car : return 0
case ..mini : return 500
case .impala : return 1000
}
}
var description : String {
switch self {
case .transport : return "semi-truck"
case .car : return "a car"
case ..mini : return "mini-cooper"
case .impala : return "Chevy Impala"
}
}
}
Of course more attributes could be added if required… No need for classes for each car type.
You can achieve this in Xojo with Extends methods.
Function Description(Extends e As Vehicle) As String
Select Case e
Case Vehicle.transport
return "semi-truck"
Case Vehicle.car
return "a car"
...
End Select
End Function
And then
dim carType as Vehicle = Vehicle.impala
MsgBox(carType.Description + " weighs " ...)
An “enum” in swift seems more similar to a class in Xojo
But since in Xojo an enum IS a datatype and you can use extends with datatypes …
you can get a lot of the functionality Swift has that way
The big downside is that its harder to wrap into a single unit
You with use a module with a const + extends methods for the enum in that module or some other mechanism
IT can make some stuff more awkward but …
It seems that almost everything in Swift has charateriscs of a “class”… Enums, Structures … and what else is kinda interesting is the ability to embed a class inside a class as well… The downside is the more I use Swift , the more I see what Xojo could be