[swift-users] Syntax for extending a struct conforming to a protocol with constraint

Austin Zheng austinzheng at gmail.com
Sun Jul 10 22:10:55 CDT 2016


You can't do this yet, but it's on the roadmap: https://github.com/apple/swift/blob/master/docs/GenericsManifesto.md#conditional-conformances- <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 at 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)
> 
> ``` swift
> 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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160710/ddec0b95/attachment.html>


More information about the swift-users mailing list