There seems to be preference for choosing enums with parameters over Structs. Personally I've always been annoyed by inability of different components to extend an "enumerated" set of values, not to mention the extra work to extract enumerated values as needed (but explained here).
Both Swift structs
and enums
create a namespace that guides autocompletion. But what I really like about using structs
is the ability to easily prototypes, instances providing default values.
With the addition of callAsFunction(...)
building on prototypes is even easier.
struct Shop {
var name: String
var type: String
func callAsFunction(_ clone_name: String) -> Shop {
var clone = self
clone.name = clone_name
return clone
}
static var cheers = Shop(name: "Cheers", type: "bar")
static var bar = Shop(name: "<bar>", type: "bar")
static var cafe = Shop(name: "<cafe>", type: "cafe")
static func custom(_ name: String, type: String) -> Shop {
Shop(name: name, type: type)
}
}
func printShops(_ shops: Shop...) {
for shop in shops {
print (shop.name, shop.type)
}
}
printShops(.cheers, .custom("Jake's", type:"saloon"), .bar("Joe's Pub"), .cafe("Cool Cafe"))