The power of Enums

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.

let carType : vehicle = .impala
print("\(carType.description) weighs \(carType.weight) ")// Chevy Impala weight 1000

Again… .this is just an illustration of a difference in Swift vs Xojo, and is not intended to dilute Norms’ blog in regards to Xojo

1 Like

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 " ...)

Personally I don’t find this a big inconvenience.

2 Likes

I knew nothing of this. This is a tip that will make my job extremely much easier. Thank you.

See a Swift tip , invokes a Xojo tip which in turn provides useful information to someone :slight_smile:

1 Like

Why would you use an Extends for that and not simply add the method to the class???

an Xojo enum doesn’t work that way

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

Yeah classes in classes is one thing I wish Xojo enabled
The compiler can handle it as far as I recall