[swift-users] How to extend a generic type with a Bool constraint?

Quinn "The Eskimo!" eskimo1 at apple.com
Wed Aug 31 12:20:47 CDT 2016


`Boolean` (the protocol) was removed in Swift 3 per SE-0109 “Remove the Boolean protocol”.

<https://github.com/apple/swift-evolution/blob/master/proposals/0109-remove-boolean.md>

You can do what you want by defining your own dummy protocol and extending `Bool` (the type) to conform to that.

protocol JensBoolean {
}

extension Bool : JensBoolean {
}

public struct Foo<T> {
}

extension Foo where T : JensBoolean {
}

There’s an obvious need to make this better — search the ’net for “generics manifesto” for some context — but that didn’t catch the Swift 3 bus.

Share and Enjoy
--
Quinn "The Eskimo!"                    <http://www.apple.com/developer/>
Apple Developer Relations, Developer Technical Support, Core OS/Hardware




More information about the swift-users mailing list