I was trying something I thought was pretty trivial today, and found myself not being able to do it. This is about implementing a protocol extension to define a default behavior. I'm not able to constraint this default implementation with a "same-type" constraint related to a "value type".
you are extending all types that conform to Chunkable where that type is also String.
However, String has not been conformed to Chunkable, so there are no types that fit these constraints. You are being reminded by the compiler to conform String to Chunkable. This is how you do that:
Let's say I want to build a framework that provides this kind of default behavior ONLY if the user of my framework makes String conform to Chunkable in its application. How can I achieve this ?
Because inside my framework, I don't want to make String conform to Chunkable. Conformance is a choice given to the user of the framework.
Yep it could absolutely be written that way. In fact, I was just trying different ways of writing this ... and was surprised not being able to write it like that.
Users should never conform a type they do not own to a protocol they do not own. This causes numerous problems down the line too involved to enumerate here. Although not banned by the compiler altogether, it is neither an encouraged nor a completely supported use case.
A user can create a subclass that they own, which they can then conform to protocols. There is no problem with conforming a type you do own to a protocol you don’t own, or a type you don’t own to a protocol you do own—in fact, that’s explicitly supported.
Additionally, that syntax was only accidentally supported for many versions of Swift, and full support was only recently enabled. Where it does not forbid non-conforming final or closed classes just like it does for structs, it’s arguably a bug.