<div dir="ltr"><div><div><div>Today I had a read through the various protocol forwarding threads in December 2015, including Matthew Johnson&#39;s proposal, and some more recent ones in which it was discussed.<br><br></div>I think the ideas are very similar in many ways - it&#39;s to improve the experience of using composition and make it easier. The protocol forwarding seems to be mostly about reducing boilerplate code, but I&#39;d really like to see composition become part of the language itself. One of the fantastic thing about the evolution of programming languages is how programmers discover many good and bad patterns, then we make new language features to incorporate the good patterns and mitigate against the bad ones. Data and pointer types, classes/objects, inheritance, abstract interfaces, to name but a few are all derived from good disciplined practice, codified such that they become easier, or required practices.<br><br>The protocol forwarding proposal is great, but it seems like the good pattern/practice it is trying to aid (composition) would still be a programmer discipline that sits atop that particular language feature. Does anyone else think encouraging composition directly as a language feature would be a good thing? A new platform-level on top of which we can develop new good and bad patterns, to continue the evolution? It&#39;s apparent from other active threads on this list that composition over inheritance is still not widely followed. I still make this same mistake. I remember going from C++ to Java (this was probably around 17 years ago now) I often wanted multiple inheritance, but the language wouldn&#39;t let me and I soon realised the error of my ways and the power of interfaces. This is the great power of a well designed language feature - it forces programmers to improve their thinking while making it easier to write better code at the same time. These days we can easily compose interfaces (in Swift for example we can adopt multiple protocols, or use protocol composition) but we still can&#39;t easily compose implementations. We tend to use delegates for composition, and auto-forwarding seems to be a good approach for making this pattern easier, but composing a type&#39;s implementation from other existing component implementations is subtly different to delegation.<br><br>So instead of wanting to inherit from a struct, or forward a protocol to avoid boilerplate, I want composition to be a natural and easy way to think of a particular type, and I want the language and compiler to nudge my thinking towards this way when I start to do something stupid. Is anyone with me on this or am I just out there talking crazy?<br><br></div>In the specifics of this case, there seem to be some issues with `Self` parameters and return types in the protocol forwarding proposal. Also, the idea of a lot of synthesized boilerplate to forward calls seems a bit messy. Perhaps it would be good if composition were able to pull in implementations like templates. So `Self` types become the type of the composed type, and the compiler decides when it needs to generate a type-specific version or when to use a generic version of an implementation. And the intention would be very clear because a component of a composition would be explicitly named something obvious like `component`.<br><br></div>It&#39;d be great to hear Matthew Johnson&#39;s thoughts on this, and anyone else who has been involved with the forwarding discussions.<br><br></div><br><div class="gmail_quote"><div dir="ltr">On Fri, 23 Jun 2017 at 14:29 Nevin Brackett-Rozinsky &lt;<a href="mailto:nevin.brackettrozinsky@gmail.com">nevin.brackettrozinsky@gmail.com</a>&gt; wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">This sounds similar to automatic protocol forward, have you looked into prior discussions on that topic here?<div><br></div><div>Nevin</div><div><br></div></div><div class="gmail_extra"><br><div class="gmail_quote"></div></div><div class="gmail_extra"><div class="gmail_quote">On Thu, Jun 22, 2017 at 10:56 PM, Jay Abbott via swift-evolution <span dir="ltr">&lt;<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>&gt;</span> wrote:<br></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div></div>Let&#39;s take a quick look at how we can achieve very simple compile-time composition in Swift today.<br><br></div><div>I want to define a `Doorman` behaviour, and I want to compose it from other behaviours that are shared by some of my other staff types:<br><br></div>```swift<br>protocol Greeter {<br>    func greet()<br>}<br>protocol Fareweller {<br>    func farewell()<br>}<br>protocol Doorman: Greeter, Fareweller {}<br>```<br><br></div><div>Great - that&#39;s my interface defined, now some implementations that I can compose my staff from:<br><br></div><div>```swift<br></div><div>protocol FriendlyGreeter: Greeter {}<br>extension FriendlyGreeter {<br>    func greet() {<br>        print(&quot;Hello and welcome&quot;)<br>    }<br>}<br><br>protocol FriendlyFareweller: Fareweller {}<br>extension FriendlyFareweller {<br>    func farewell() {<br>        print(&quot;I bid thee farewell&quot;)<br>    }<br>}<br><br>protocol InsultingGreeter: Greeter {}<br>extension InsultingGreeter {<br>    func greet() {<br>        print(&quot;You make me sick&quot;)<br>    }<br>}<br><br>protocol InsultingFareweller: Fareweller {}<br>extension InsultingFareweller {<br>    func farewell() {<br>        print(&quot;Get lost&quot;)<br>    }<br>}<br>```<br><br>Now we have two kinds of `Greeter` and two kinds of `Fareweller` that can be used to compose different `Doorman` types (and potentially other staff types). Here&#39;s two examples:<br><br></div><div>```swift<br></div><div>struct FriendlyDoorman: Doorman, FriendlyGreeter, FriendlyFareweller {}<br>struct TrickingDoorman: Doorman, FriendlyGreeter, InsultingFareweller {}<br>```<br><br>I can instantiate and make use of these to perform their defined behaviours:<br><br></div><div>```swift<br></div><div>let friendly: Doorman = FriendlyDoorman()<br>let tricking: Doorman = TrickingDoorman()<br>friendly.greet() // Hello and welcome<br>friendly.farewell() // I bid thee farewell<br>tricking.greet() // Hello and welcome<br>tricking.farewell() // Get lost<br>```<br><br>It works! But there are some things that could be nicer:<br>* I don&#39;t really want `***Greeter` or `***Fareweller` to be sub-protocols at all, these are supposed to be implementations - the only reason they are protocols is so I can extend them with a default implementation and then use more than one of them to compose my actual `Doorman` types. This clutters up the namespace with unnecessary protocols, that have the same interface as their parent.<br></div><div>* Since the `***Doorman` types need to be instantiable, they are structs. I couldn&#39;t compose a `LobbyMultiTasker` from a `FriendlyDoorman` and a `GrumpyPorter` at compile-time, the same easy way I composed the `***Doorman` types. The manual solution would be to add properties for `doormanDelegate` and `porterDelegate` and assign appropriate instances (run-time composition), then add the boiler-plate to pass on the `LobbyMultiTasker` behaviour to these delegates. This is also how the compiler could implement this feature.<br></div><div>* Actually providing the implementations is optional for the protocols (the extensions can happily be missing), meaning any error messages will appear in the types that fail to fully implement the protocols, instead of here in my `***Greeter` implementation.<br></div><div><br></div><div>So I&#39;d like to discuss the possibility of a new category of types called `component`, to allow compile-time composition; composition as part of the language. The `***Greeter` and `***Fareweller` might look like this:<br><br><div>```swift<br></div><div>component FriendlyGreeter: Greeter {<br>    func greet() {<br>        print(&quot;Hello and welcome&quot;)<br>    }<br>}<br><br>component FriendlyFareweller: Fareweller {<br>    func farewell() {<br>        print(&quot;I bid thee farewell&quot;)<br>    }<br>}<br><br>component InsultingGreeter: Greeter {<br>    func greet() {<br>        print(&quot;You make me sick&quot;)<br>    }<br>}<br><br>component InsultingFareweller: Fareweller {<br>    func farewell() {<br>        print(&quot;Get lost&quot;)<br>    }<br>}<br>```<br></div><div><br>And the `TrickingDoorman` might look like this:<br><br></div><div>```swift<br></div><div>component TrickingDoorman: Doorman⎄(FriendlyGreeter, InsultingFareweller) {<br></div><div>    // optionally override any Doorman-defined functions<br></div><div>}<br><br><br></div><div>```<br><br></div><div>Here&#39;s how I think they would work:<br><br></div><div>* Components must conform to at least one protocol.<br></div><div>* Components must provide an implementation for all the things from the protocols - no part of it is &#39;abstract&#39; - and they can&#39;t provide extra things (although it might be useful to allow some kind of config-values, but I&#39;d say initially keep it simple).<br></div><div>* Components can be composed of other components and override some/all of the borrowed behaviour. This should provide well defined multiple-inheritence semantics (not sure of details), some syntax would be required to allow the compiler to totally flatten the component, selecting which &quot;parent&quot; implementations to use if needed to satisfy all the protocols. Only the functions from the explicit protocols are pulled in, not everything implemented in the &quot;parent&quot; components.<br></div><div>* Components can be instantiated and have value-semantics, like structs (they are basically structs with extra features/checks). They can therefore be used at compile-time but also at run-time for example `delegate = MyComponent()` - so composed behaviour can be either static or dynamic, also existing protocol-based `delegate` variables can have a component instance assigned.<br>* Classes, structs, and enums can NOT override functions implemented in a component, when they compose themselves from those components. To do this, create a sub-component and override the method, then compose your type from that instead.<br><br></div><div>Other benefits:<br></div><div>* I think this would encourage more single-responsibility by default. Because when designing a protocol, people tend to forget composition. Enforcing must-implement-everything would remind/encourage API designers to split protocols up, especially if they want to provide a default implementation for some but not all of the functions.<br></div><div>* Tidier code, with much clearer intention (component vs extension in particular).<br></div><div>* Easy re-use without having pass-through boilerplate code.<br></div><div>* Brings well-defined-ness to default implementations, which can sometimes be unclear whether it&#39;s an actual default implementation or an empty placeholder<br></div><div><br></div> I haven&#39;t thought of everything here, obviously - and I&#39;m tired, so please poke holes and supply constructive corrections :)<br><div><br>Any suggestions for the &quot;composed-of&quot; syntax for when a type wants to utilise a component would be welcome. I used `Doorman⎄(FriendlyGreeter, InsultingFareweller)` in the example above, where ⎄ is the composition symbol that I just discovered, but this is just intended to be a placeholder.<br></div><div><br><br></div></div></div></div>
<br></blockquote></div></div><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">_______________________________________________<br>
swift-evolution mailing list<br>
<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a><br>
<a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br>
<br></blockquote></div><br></div>
</blockquote></div>