"Neither type in same-type constraint ... refers to a generic parameter or associated type"

Seemingly legal code in Swift 4.2:

extension Optional {
    public func compact<T>() -> T? where Optional<Wrapped> == T?? {
        return self ?? .none
    }

    public func compact<T>() -> T? where Optional<Wrapped> == T??? {
        return (self ?? .none) ?? .none
    }

    public func compact<T>() -> T? where Optional<Wrapped> == T???? {
        return ((self ?? .none) ?? .none) ?? .none
    }
}

Now yields warnings in Swift 5. Is this a bug?

Never mind, fixed it!

extension Optional {
    public func compact<T>() -> T? where Wrapped == T? {
        return self ?? .none
    }

    public func compact<T>() -> T? where Wrapped == T?? {
        return (self ?? .none) ?? .none
    }

    public func compact<T>() -> T? where Wrapped == T??? {
        return ((self ?? .none) ?? .none) ?? .none
    }
}
1 Like
Terms of Service

Privacy Policy

Cookie Policy