snej
(Jens Alfke)
1
I’ve got a simple generic struct that wraps an instance of its parameter type.
public struct check<T> {
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<Bool> {
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
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 <swift-users@swift.org> wrote:
I’ve got a simple generic struct that wraps an instance of its parameter type.
public struct check<T> {
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<Bool> {
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
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.
I think it's on the list of things they'd like to do to generics in the future, but it's a very long list and they might not get to that particular one.
In short, though, my understanding is that there is no principled reason for this limitation; it simply isn't supported yet.
···
--
Brent Royal-Gordon
Architechies