I have a custom type Banana
and I would like to create an extension of Array
(or, If I have to, Sequence
) of Banana
to conform to the protocol CustomStringConvertible
so that calling description
on the array of Banana
would return "A bunch of bananas". Is this possible and, if so, how would I go about doing this?
How do I create an extension to allow an array of a custom type to conform to a protocol?
597 views Asked by Josh Paradroid At
2
Short answer: no.
You can constrain an extension, but a constrained extension can't contain an inheritance clause (the Swift proposal @Code Different linked above is exactly what you're looking for).
One workaround would be to make the constrained extension, but just add your own property, rather than having it conform to
CustomStringConvertible
.Worth noting, too, that
Array
already conforms toCustomStringConvertible
.