Bool isn’t a class (or protocol), so nothing can inherit from it.
Until Swift gains the ability to extend generic types where their parameters
are *equal* to concrete types, as opposed to where they *inherit from or
conform to* something, I think the best you can do is this:
public extension check where T: BooleanType {
public func isTrue() { XCTAssertTrue(actual.boolValue) }
}
I *think* the feature you’re trying to use is on the todo list for Swift 3, but
the last time I said that, I was completely wrong. The swift-evolution mailing
list probably has more information.
Anyway, I hope that helps.
- Dave Sweeris
> On Apr 12, 2016, at 11:11 AM, Jens Alfke via swift-users
> wrote:
>
> I’ve got a simple generic struct that wraps an instance of its parameter type.
>
> public struct check {
> let actual: T
> public init(_ a: T) {actual = a}
> ...
>
> Now I want to add a method that only works with a specific type, Bool:
> public func isTrue(){XCTAssertTrue(actual)}
>
> As I’d expect, the compiler doesn’t allow this: “Cannot convert value of type
> ’T’ to expected argument type ‘Bool’”.
> So I’m trying to put this method in an extension that constrains T:
>
> public extension check {
> public func isTrue(){XCTAssertTrue(actual)}
> }
>
> This fails with "Constrained extension must be declared on the unspecialized
> generic type 'check' with constraints specified by a 'where’ clause”. OK, so
> I add a ‘where’ clause:
>
> public extension check where T: Bool {
> public func isTrue(){XCTAssertTrue(actual)}
> }
>
> This produces the error "type 'T' constrained to non-protocol type ‘Bool’”.
> This confuses me — why is constraining to a non-protocol type an error? The
> Swift Programming Language says “the ‘where’ clause … can express the
> constraints that a generic type T inherits from a class C”.
>
> —Jens
> ___
> swift-users mailing list
> swift-users@swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
___
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users