Re:Limitations of protocols in Swift
by panic on Wednesday December 16, 2015 @06:06AM
(#10743509)
He mentions a plug-in API in the second linked post. Enums would be a good solution, but they can't be extended with more cases by a plug-in.
[ Parent
| on HN ]
Re:Limitations of protocols in Swift
by Manishearth on Wednesday December 16, 2015 @06:22AM
(#10743558)
Yeah, that's when you would use trait objects in Rust (and virtual dispatch protocols in Swift?), but you still probably won't use them exactly the same way.
Re:Limitations of protocols in Swift
by alexjarvis on Wednesday December 16, 2015 @06:39AM
(#10743617)
extensions let you do this with enums (as well as protocols):
enum Car {
case Red
case Blue
}
extension Car {
func hello() {
print("hello")
}
}
let x = Car.Blue
x.hello()
https://developer.apple.com/library/ios/documentation/Swift/...edit: although you can't add a new case to the enum.
Re:Limitations of protocols in Swift
by Manishearth on Wednesday December 16, 2015 @06:43AM
(#10743626)
> although you can't add a new case to the enum.
That's the point. We all know you can add impls or extensions.