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

Zhao Xin owenzx at gmail.com
Sun Jul 10 22:08:26 CDT 2016


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 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/20160711/f86046aa/attachment.html>


More information about the swift-users mailing list