Syntax for extending a struct conforming to a protocol with constraint

Hello, every one. I want to extend a struct to conform to a protocol, while it’s itself a generic and needs a constraint. So I wrote this: (Swift 3)

struct Polynomial<Field: Number> {
	//definition goes here
}

extension Polynomial: CustomStringConvertible where Field: CustomStringConvertible {
	//implementation goes here
}

Then I receive an error message from the compiler:
Extension of type 'Polynomial' with constraints cannot have an inheritance clause

This is the only way I know to do this(theoretically), but unfortunately it doesn’t work.

So, how can I achieve this? What syntax should be employed?

You can't do this yet, but it's on the roadmap: https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#conditional-conformances-

Austin

···

On Jul 9, 2016, at 10:54 PM, 褚晓敏 via swift-users <swift-users@swift.org> wrote:

Hello, every one. I want to extend a struct to conform to a protocol, while it’s itself a generic and needs a constraint. So I wrote this: (Swift 3)

struct Polynomial<Field: Number> {
	//definition goes here
}

extension Polynomial: CustomStringConvertible where Field: CustomStringConvertible {
	//implementation goes here
}

Then I receive an error message from the compiler:
Extension of type 'Polynomial' with constraints cannot have an inheritance clause

This is the only way I know to do this(theoretically), but unfortunately it doesn’t work.

So, how can I achieve this? What syntax should be employed?
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

How about this?

struct Polynomial<Field: Number, CustomStringConvertible > {

        //definition goes here
}
extension Polynomial: CustomStringConvertible {
        //implementation goes here
}

Zhaoxin

···

On Sun, Jul 10, 2016 at 1:54 PM, 褚晓敏 via swift-users <swift-users@swift.org> wrote:

Hello, every one. I want to extend a struct to conform to a protocol,
while it’s itself a generic and needs a constraint. So I wrote this: (Swift
3)

struct Polynomial<Field: Number> {
        //definition goes here
}

extension Polynomial: CustomStringConvertible where Field:
CustomStringConvertible {
        //implementation goes here
}

Then I receive an error message from the compiler:
Extension of type 'Polynomial' with constraints cannot have an inheritance
clause

This is the only way I know to do this(theoretically), but unfortunately
it doesn’t work.

So, how can I achieve this? What syntax should be employed?
_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users