<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">I’d like to pitch the following two language changes. Both of them are technically possible today if you manually write thunks for the relevant protocol requirements, but it would be nice if we allowed them to be written directly:<div class=""><br class=""></div><div class="">1) Allow closures to satisfy function requirements in protocols</div><div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(246, 248, 250); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal; color: rgb(36, 41, 46);" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">protocol</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">MyProtocol</span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">run</span>(<span class="pl-smi" style="box-sizing: border-box; color: rgb(51, 51, 51);">param</span>: <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>) <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span>
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">struct</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">MyStruct</span> : <span class="pl-e" style="box-sizing: border-box; color: rgb(121, 93, 163);">MyProtocol </span>{
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">var</span> run <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">:</span> (<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>)<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span><span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">String</span> <span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);"><span class="pl-c" style="box-sizing: border-box;">//</span> Satisfies requirement MyProtocol.run</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);"></span>}</pre><div class=""><br class=""></div></div></blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class="">Among other things, it would make writing type-erased wrappers in the style of AnyCollection much easier. The only obvious niggle is that the argument label wouldn’t be required when invoking the closure directly. The labels have no type-system significance, but it does make it subtly easier to write less generic code than you intend do. We could solve this by having code-completion favour protocol methods in this situation, or simply to require the label when invoking a closure which implements a known protocol requirement.</blockquote><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><br class=""></div></blockquote>2) Allow functions with default parameters to satisfy function requirements in protocols<div class=""><br class=""></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div class=""><pre style="box-sizing: border-box; font-family: SFMono-Regular, Consolas, 'Liberation Mono', Menlo, Courier, monospace; font-size: 13.600000381469727px; margin-top: 0px; margin-bottom: 0px; line-height: 1.45; word-wrap: normal; padding: 16px; overflow: auto; background-color: rgb(246, 248, 250); border-top-left-radius: 3px; border-top-right-radius: 3px; border-bottom-right-radius: 3px; border-bottom-left-radius: 3px; word-break: normal; color: rgb(36, 41, 46);" class=""><span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">protocol</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">Sportsplayer</span> {
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">goalsScored</span>() <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">-></span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Int</span>
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">struct</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">SoccerPlayer</span>: <span class="pl-e" style="box-sizing: border-box; color: rgb(121, 93, 163);">Sportsplayer </span>{
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">struct</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">GoalType</span> : <span class="pl-e" style="box-sizing: border-box; color: rgb(121, 93, 163);">RawOptionSet </span>{
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> Shot <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">GoalType</span>(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0x1</span>)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> Volley <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">GoalType</span>(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0x10</span>)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> Header <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">GoalType</span>(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0x100</span>)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Any</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">GoalType</span>(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0x111</span>)
}
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);"><span class="pl-c" style="box-sizing: border-box;">//</span> Default value .Any means this conforms to Sportsplayer</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);"></span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">goalsScored</span>(<span class="pl-smi" style="box-sizing: border-box; color: rgb(51, 51, 51);"><span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">type</span></span>: GoalType <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> .<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Any</span>) {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);"><span class="pl-c" style="box-sizing: border-box;">//</span>...</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);"></span> }
}
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">struct</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">Footballer</span>: <span class="pl-e" style="box-sizing: border-box; color: rgb(121, 93, 163);">Sportsplayer </span>{
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">struct</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">GoalType</span> : <span class="pl-e" style="box-sizing: border-box; color: rgb(121, 93, 163);">RawOptionSet </span>{
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> Touchdown <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">GoalType</span>(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0x1</span>)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> FieldGoal <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">GoalType</span>(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0x10</span>)
<span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">static</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">let</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Any</span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> <span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">GoalType</span>(<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">0x11</span>)
}
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);"><span class="pl-c" style="box-sizing: border-box;">//</span> Default value .Any means this conforms to Sportsplayer</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);"></span> <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">func</span> <span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">goalsScored</span>(<span class="pl-smi" style="box-sizing: border-box; color: rgb(51, 51, 51);"><span class="pl-en" style="box-sizing: border-box; color: rgb(121, 93, 163);">type</span></span>: GoalType <span class="pl-k" style="box-sizing: border-box; color: rgb(167, 29, 93);">=</span> .<span class="pl-c1" style="box-sizing: border-box; color: rgb(0, 134, 179);">Any</span>) {
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);"><span class="pl-c" style="box-sizing: border-box;">//</span>...</span>
<span class="pl-c" style="box-sizing: border-box; color: rgb(150, 152, 150);"></span> }
}</pre><div class=""><br class=""></div></div><div class="">I often find that I want to add some optional, implementation-specific parameter to a function which implements a protocol requirement. That’s currently not possible, and it’s a bit annoying.</div><div class=""><br class=""></div><div class=""><br class=""></div></blockquote>- Karl<br class=""><div class=""><br class=""></div></body></html>