How to extend a generic type with a Bool constraint?

I have a generic type, and I want to add some methods that are available when the type parameter is Bool. (This is in Xcode 8 beta 6, btw.)

public struct foo<T> {
  ...
}

extension foo where T: Bool {
  …
}

The above fails to compile, with "type 'T' constrained to non-protocol type ‘Bool’”. Is that an error? The book doesn’t say that type constraints have to be to protocols; in fact the example in the section “Type Constraint Syntax” shows a type constraint that requires T to be a subclass of SomeClass.*

OK, so I’ll find a protocol for “boolean-like” values that’s implemented by Bool. The Swift reference for `Bool` in Xcode doesn’t list anything. Swiftdoc.org <http://swiftdoc.org/&gt; shows a protocol `Boolean` that looks like exactly what I want, but my compiler’s never heard of it; is this something that snuck in post-3.0? If I switch Swiftdoc.org <http://swiftdoc.org/&gt; back to Swift 2.2, it shows an equivalent protocol called `BooleanType`, but if I try to use that, the compiler says "'BooleanType' has been renamed to ‘Bool’”.

At this point I threw my laptop at the wall.
Not really, but I started to miss the stability and simplicity of C++… :-p

How the @$*% do I do this?

—Jens

* In fact the book never seems to talk about type constraints in generic type extensions. It covers each of the two concepts, but not how to use them together.

You'll need to specify a protocol where you're currently specifying "Bool".
A generic constraint can't take a concrete type as an argument. So you
could do this:

protocol BooleanType {}
extension Bool: BooleanType {}

extension foo where T: BooleanType {
   ...
}

If you don't like this, you can define a protocol with an associatedtype
and use a protocol extension to add this functionality to foo<T> instead.
E.g.:

protocol bar {
    associatedtype X
}

extension bar where X == Bool {
    ...
}

public struct foo<T>: bar {
    typealias X = T

    ...
}

···

On Tue, Aug 30, 2016 at 6:15 PM, Jens Alfke via swift-users < swift-users@swift.org> wrote:

I have a generic type, and I want to add some methods that are available
when the type parameter is Bool. *(This is in Xcode 8 beta 6, btw.)*

public struct foo<T> {
...
}

extension foo where T: Bool {

}

The above fails to compile, with "type 'T' constrained to non-protocol
type ‘Bool’”. Is that an error? The book doesn’t say that type constraints
have to be to protocols; in fact the example in the section “Type
Constraint Syntax” shows a type constraint that requires T to be a subclass
of SomeClass.*

OK, so I’ll find a protocol for “boolean-like” values that’s implemented
by Bool. The Swift reference for `Bool` in Xcode doesn’t list anything.
Swiftdoc.org shows a protocol `Boolean` that looks like exactly what I
want, but my compiler’s never heard of it; is this something that snuck in
post-3.0? If I switch Swiftdoc.org back to Swift 2.2, it shows an
equivalent protocol called `BooleanType`, but if I try to use that, the
compiler says "'BooleanType' has been renamed to ‘Bool’”.

At this point I threw my laptop at the wall.
Not really, but I started to miss the stability and simplicity of C++… :-p

How the @$*% do I do this?

—Jens

* In fact the book never seems to talk about type constraints in generic
type extensions. It covers each of the two concepts, but not how to use
them together.

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

`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&gt;

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/&gt;
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

OK … this should be described in the book, though. (Any doc writers listening? Or is there a procedure for requesting improvements to the book?)

I’m also bemused that (as I said) there is a boolean protocol in Swift 2.2, and a different one in master (according to swiftdoc.org <http://swiftdoc.org/&gt;\), but none in Swift 3.0 (as of Xcode 8 beta 6). Is this just a glitch that snuck into the release? Or does the Swift in Xcode predate an API freeze?

—Jens

···

On Aug 31, 2016, at 10:21 AM, Dan Loewenherz <dan@lionheartsw.com> wrote:

You'll need to specify a protocol where you're currently specifying "Bool". A generic constraint can't take a concrete type as an argument.

To answer my own question: swiftdoc.org <http://swiftdoc.org/&gt; is out of date; it hasn’t been updated since June 18 <https://github.com/SwiftDocOrg/SwiftDocOrg.github.io/commits/master&gt; :( I have filed an issue <Issues · SwiftDocOrg/swiftdoc.org · GitHub.

—Jens

···

On Aug 31, 2016, at 11:09 AM, Jens Alfke <jens@mooseyard.com> wrote:

I’m also bemused that (as I said) there is a boolean protocol in Swift 2.2, and a different one in master (according to swiftdoc.org <http://swiftdoc.org/&gt;\), but none in Swift 3.0 (as of Xcode 8 beta 6). Is this just a glitch that snuck into the release? Or does the Swift in Xcode predate an API freeze?

(Any doc writers listening? Or is there a procedure for requesting improvements to the book?)

If you file a Radar bug against the doc it will go to the right folks [1].

<https://developer.apple.com/bug-reporting/&gt;

I’m also bemused …

This is a linear progression in time:

1. `BooleanType` is the name in Swift 2.

2. `Boolean` is the post ‘great renaming’ name in Swift 3.

3. Currently it is unavailable per SE-0109.

Share and Enjoy

···

On 31 Aug 2016, at 19:09, Jens Alfke via swift-users <swift-users@swift.org> wrote:
--
Quinn "The Eskimo!" <http://www.apple.com/developer/&gt;
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

[1] Other stuff might works as well but Radar will definitely work.