<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class="">So the topic of global functions like min/max came up on the thread about adding a standard clamp method, and it got me to thinking whether there was a better way to define most global methods.<div class=""><br class=""></div><div class="">Currently for example there are two global functions min and max; very useful, and don't make much sense as instance methods, but they're not as easily discoverable as a static method declared under relevant type(s). We could get around this by defining such functions both as a static method, and as a global function, but it means duplicate code and by convention only.</div><div class=""><br class=""></div><div class="">An alternative is to remove the need for duplicate methods using a new global keyword for a method declaration, telling Swift to define it as both a static method <b class="">and</b> a global function. This allows the function to be grouped logically under a type, making it a bit neater, but without losing the benefit of the global definition. For example:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>protocol Comparable {</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>global func min(_ a:Self, _ b:Self) -&gt; Self { return a &lt; b ? a : b }</font></div><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space: pre;">                </span>global func max(_ a:Self, _ b:Self) -&gt; Self { return a &gt; b ? a : b }</font><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><br class=""></div><div class="">With this single definition both of the following are now valid:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let min = Int.min(1, 3)</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>let max = max(5, 10)</font></div><div class=""><br class=""></div><div class="">In the second case, Swift looks at all global definitions for "max" in order to locate the best match, leading it to Int.max.</div><div class=""><br class=""></div><div class="">It's a relatively small change, but helps with neatness. It may also be good for consistency if we ever get the ability to define operators within types (though I'm not sure where we are with that?), as they could potentially just use the same format like-so:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>struct MyType : Equatable {</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">                </span>global func == (_ a:MyType, _ b:MyType) -&gt; Bool { /* Determine equality */ }</font></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>}</font></div><div class=""><br class=""></div><div class="">I just think it's a neater way to do this than currently requiring separate declarations for the static and global versions, especially since one usually just calls the other anyway, anyone have any thoughts?</div><div class=""><br class=""></div><div class="">I normally argue against new keywords, but in this case it may be justified, as I considered an attribute but it would only end up requiring static anyway, so it seemed neater to just have a new category "above" static that is both static and global, since a global non-static doesn't make any sense. However, one advantage of a @global attribute is that it could potentially take a different name for the global function, so I could (but probably wouldn't) define something like:</div><div class=""><br class=""></div><div class=""><font face="Monaco" class=""><span class="Apple-tab-span" style="white-space:pre">        </span>@global("min") static func smallerOfTwo(_ a:Self, _ b:Self) -&gt; Self { return a &lt; b ? a : b }</font></div><div class=""><br class=""></div><div class="">So here, although the method name is smallerOfTwo, it is exposed globally as "min". Though I don't know much need there would be for something like that.</div></body></html>