<div dir="ltr">This is the full code:<div><br></div><div>
<p class=""><span class="">//</span></p>
<p class=""><span class="">// Box.swift</span></p>
<p class=""><span class="">// Project</span></p>
<p class=""><span class="">//</span></p>
<p class=""><span class="">// Created by James Campbell on 06/01/2016.</span></p>
<p class=""><span class="">//</span></p>
<p class=""><span class="">//</span></p>
<p class=""><span class="">// This File implements the Box protocol for Type Safe Alogrithm Types and other useful things.</span></p>
<p class=""><span class=""></span><br></p>
<p class=""><span class="">import</span><span class=""> Foundation</span></p>
<p class=""><span class=""></span><br></p>
<p class=""><span class="">//MARK:- Box</span></p>
<p class=""><span class=""></span><br></p>
<p class=""><span class="">protocol</span><span class=""> Box: </span><span class="">CustomStringConvertible</span><span class="">, </span><span class="">CustomDebugStringConvertible</span><span class=""> {</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">typealias</span><span class=""> FloatLiteralType = Double</span></p>
<p class=""><span class=""> </span><span class="">typealias</span><span class=""> IntegerLiteralType = Int</span></p>
<p class=""><span class=""> </span><span class="">typealias</span><span class=""> BoxType = Any</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">var</span><span class=""> value: </span><span class="">BoxType</span><span class=""> { </span><span class="">get</span><span class=""> </span><span class="">set</span><span class=""> }</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">init</span><span class="">()</span></p>
<p class=""><span class=""> </span><span class="">init</span><span class="">(</span><span class="">_</span><span class=""> value: </span><span class="">BoxType</span><span class="">)</span></p>
<p class=""><span class="">}</span></p>
<p class=""><span class=""></span><br></p>
<p class=""><span class="">extension</span><span class=""> </span><span class="">Box</span><span class=""> </span><span class="">where</span><span class=""> BoxType: CustomStringConvertible {</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">var</span><span class=""> description: </span><span class="">String</span><span class=""> {</span></p>
<p class=""><span class=""> </span><span class="">return</span><span class=""> </span><span class="">self</span><span class="">.</span><span class="">value</span><span class="">.</span><span class="">description</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">var</span><span class=""> debugDescription: </span><span class="">String</span><span class=""> {</span></p>
<p class=""><span class=""> </span><span class="">return</span><span class=""> </span><span class="">"</span><span class="">\</span><span class="">(</span><span class="">self</span><span class="">.</span><span class="">value</span><span class="">.</span><span class="">description</span><span class="">)"</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class="">}</span></p>
<p class=""><span class=""></span><br></p>
<p class=""><span class="">//MARK: FloatingPointBox</span></p>
<p class=""><span class=""></span><br></p>
<p class=""><span class="">protocol</span><span class=""> FloatingPointBox: </span><span class="">Box</span><span class="">, </span><span class="">FloatLiteralConvertible</span><span class="">, </span><span class="">IntegerLiteralConvertible</span><span class=""> {</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">typealias</span><span class=""> BoxType = Double</span></p>
<p class=""><span class=""> </span><span class="">typealias</span><span class=""> FloatLiteralConvertible = Double</span></p>
<p class=""><span class=""> </span><span class="">typealias</span><span class=""> IntegerLiteralConvertible = Int</span></p>
<p class=""><span class="">}</span></p>
<p class=""><span class=""></span><br></p>
<p class=""><span class="">extension</span><span class=""> </span><span class="">Box</span><span class=""> </span><span class="">where</span><span class=""> </span><span class="">Self</span><span class="">.BoxType == Double {</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">init</span><span class="">(</span><span class="">_</span><span class=""> value: </span><span class="">Double</span><span class="">) {</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">()</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">value</span><span class=""> = value</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">init</span><span class="">(</span><span class="">_</span><span class=""> value: </span><span class="">Int</span><span class="">) {</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">()</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">value</span><span class=""> = </span><span class="">Double</span><span class="">(value)</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">init</span><span class="">(</span><span class="">_</span><span class=""> value: </span><span class="">UInt32</span><span class="">) {</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">()</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">value</span><span class=""> = </span><span class="">Double</span><span class="">(value)</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class="">}</span></p>
<p class=""><span class=""></span><br></p>
<p class=""><span class="">extension</span><span class=""> </span><span class="">FloatLiteralType</span><span class=""> {</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">init</span><span class=""><T: </span><span class="">Box</span><span class=""> </span><span class="">where</span><span class=""> </span><span class="">T</span><span class="">.</span><span class="">BoxType</span><span class=""> == </span><span class="">Double</span><span class=""> >(</span><span class="">_</span><span class=""> box: </span><span class="">T</span><span class="">) {</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(box.</span><span class="">value</span><span class="">)</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">init</span><span class=""><T: </span><span class="">Box</span><span class=""> </span><span class="">where</span><span class=""> </span><span class="">T</span><span class="">.</span><span class="">BoxType</span><span class=""> == </span><span class="">Int</span><span class=""> >(</span><span class="">_</span><span class=""> box: </span><span class="">T</span><span class="">) {</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(box.</span><span class="">value</span><span class="">)</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class="">}</span></p>
<p class=""><span class=""></span><br></p>
<p class=""><span class="">extension</span><span class=""> </span><span class="">CGFloat</span><span class=""> {</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">init</span><span class=""><T: </span><span class="">Box</span><span class=""> </span><span class="">where</span><span class=""> </span><span class="">T</span><span class="">.</span><span class="">BoxType</span><span class=""> == </span><span class="">Double</span><span class=""> >(</span><span class="">_</span><span class=""> box: </span><span class="">T</span><span class="">) {</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(box.</span><span class="">value</span><span class="">)</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">init</span><span class=""><T: </span><span class="">Box</span><span class=""> </span><span class="">where</span><span class=""> </span><span class="">T</span><span class="">.</span><span class="">BoxType</span><span class=""> == </span><span class="">Int</span><span class=""> >(</span><span class="">_</span><span class=""> box: </span><span class="">T</span><span class="">) {</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(box.</span><span class="">value</span><span class="">)</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class="">}</span></p>
<p class=""><span class=""></span><br></p>
<p class=""><span class="">//Adding FloatLiteralConvertible, IntegerLiteralConvertible</span></p>
<p class=""><span class=""></span><br></p>
<p class=""><span class="">extension</span><span class=""> </span><span class="">FloatingPointBox</span><span class=""> </span><span class="">where</span><span class=""> </span><span class="">Self</span><span class="">.BoxType == Double, </span><span class="">Self</span><span class="">.FloatLiteralConvertible == Double {</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">init</span><span class="">(floatLiteral value: </span><span class="">Double</span><span class="">) {</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(value)</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">init</span><span class="">(integerLiteral value: </span><span class="">Int</span><span class="">) {</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(value)</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class=""> </span></p>
<p class=""><span class=""> </span><span class="">init</span><span class=""><T: </span><span class="">IntegerType</span><span class="">>(</span><span class="">_</span><span class=""> value: </span><span class="">T</span><span class="">) {</span></p>
<p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(value)</span></p>
<p class=""><span class=""> }</span></p>
<p class=""><span class="">}</span></p><p class=""><span class=""><br></span></p><p class=""><span class="">//</span></p><p class=""><span class="">// Angles.swift</span></p><p class=""><span class="">// Sup</span></p><p class=""><span class="">//</span></p><p class=""><span class="">// Created by James Campbell on 22/12/2015.</span></p><p class=""><span class="">// Copyright © 2015 Sup. All rights reserved.</span></p><p class=""><span class="">//</span></p><p class=""><span class="">// This File defines representations of Degrees and Radians as Type Safe Alogrithm Types</span></p><p class=""><span class=""></span><br></p><p class=""><span class="">import</span><span class=""> Foundation</span></p><p class=""><span class=""></span><br></p><p class=""><span class="">//MARK:- Degree</span></p><p class=""><span class=""></span><br></p><p class=""><span class="">struct</span><span class=""> Degree: </span><span class="">FloatingPointBox</span><span class=""> {</span></p><p class=""><span class=""> </span></p><p class=""><span class=""> </span><span class="">var</span><span class=""> value: </span><span class="">Double</span><span class=""> = </span><span class="">0</span></p><p class=""><span class=""> </span></p><p class=""><span class=""> </span><span class="">init</span><span class="">()</span></p><p class=""><span class=""> {</span></p><p class=""><span class=""> }</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br></p><p class=""><span class="">protocol</span><span class=""> DegreeConvertiable {</span></p><p class=""><span class=""> </span></p><p class=""><span class=""> </span><span class="">init</span><span class="">(degreeLiteral value: </span><span class="">Degree</span><span class="">)</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br></p><p class=""><span class="">extension</span><span class=""> </span><span class="">Degree</span><span class="">: </span><span class="">RadianConvertiable</span><span class=""> {</span></p><p class=""><span class=""> </span></p><p class=""><span class=""> </span><span class="">init</span><span class="">(radianLiteral value: </span><span class="">Radian</span><span class="">) {</span></p><p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">value</span><span class=""> = </span><span class="">Double</span><span class="">(value) * </span><span class="">180.0</span><span class=""> / </span><span class="">M_PI</span></p><p class=""><span class=""> }</span></p><p class=""><span class=""> </span></p><p class=""><span class=""> </span><span class="">init</span><span class="">(</span><span class="">_</span><span class=""> value: </span><span class="">Radian</span><span class="">) {</span></p><p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(radianLiteral: value)</span></p><p class=""><span class=""> }</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br></p><p class=""><span class="">//MARK:- Radian</span></p><p class=""><span class=""></span><br></p><p class=""><span class="">struct</span><span class=""> Radian: </span><span class="">FloatingPointBox</span><span class=""> {</span></p><p class=""><span class=""> </span></p><p class=""><span class=""> </span><span class="">var</span><span class=""> value: </span><span class="">Double</span><span class=""> = </span><span class="">0</span></p><p class=""><span class=""> </span></p><p class=""><span class=""> </span><span class="">init</span><span class="">()</span></p><p class=""><span class=""> {</span></p><p class=""><span class=""> }</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br></p><p class=""><span class="">protocol</span><span class=""> RadianConvertiable {</span></p><p class=""><span class=""> </span></p><p class=""><span class=""> </span><span class="">init</span><span class="">(radianLiteral value: </span><span class="">Radian</span><span class="">)</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br></p><p class=""><span class="">extension</span><span class=""> </span><span class="">Radian</span><span class="">: </span><span class="">DegreeConvertiable</span><span class=""> {</span></p><p class=""><span class=""> </span></p><p class=""><span class=""> </span><span class="">init</span><span class="">(degreeLiteral value: </span><span class="">Degree</span><span class="">) {</span></p><p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">value</span><span class=""> = </span><span class="">Double</span><span class="">(value) * </span><span class="">M_PI</span><span class=""> / </span><span class="">180.0</span></p><p class=""><span class=""> }</span></p><p class=""><span class=""> </span></p><p class=""><span class=""> </span><span class="">init</span><span class="">(</span><span class="">_</span><span class=""> value: </span><span class="">Degree</span><span class="">) {</span></p><p class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(degreeLiteral: value)</span></p><p class=""><span class=""> }</span></p><p class=""><span class="">
</span></p><p class=""><span class="">}</span></p><p class=""><span class="">let someOptiona: Degreee? = nil</span></p><p class="">
</p><p class=""><span class=""> let degree = someOptional ?? Degree(arc4random_uniform(360))</span></p></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 6, 2016 at 6:57 PM, Félix Cloutier <span dir="ltr"><<a href="mailto:felixcca@yahoo.ca" target="_blank">felixcca@yahoo.ca</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>I either don't understand or can't reproduce the issue. This code:</div><div><br></div><div></div><blockquote type="cite"><div>class Box {</div><div><span style="white-space:pre-wrap">        </span>let value: Int</div><div><span style="white-space:pre-wrap">        </span></div><div><span style="white-space:pre-wrap">        </span>init(value: Int) {</div><div><span style="white-space:pre-wrap">                </span>self.value = value</div><div><span style="white-space:pre-wrap">        </span>}</div><div>}</div><div><br></div><div>let test: UInt32 = 4</div><div>let box = Box(value: test)</div></blockquote><div><br></div><div>does not compile ("foo.swift:10:22: error: cannot convert value of type 'UInt32' to expected argument type 'Int'").</div><span class="HOEnZb"><font color="#888888"><div>
<br><span style="color:rgb(0,0,0);font-family:'Lucida Grande';font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;display:inline!important;float:none">Félix</span>
</div></font></span><div><div class="h5">
<br><div><blockquote type="cite"><div>Le 6 janv. 2016 à 13:17:03, James Campbell <<a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a>> a écrit :</div><br><div><div dir="ltr" style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><p><span>protocol</span><span><span> </span>Box:<span> </span></span><span>CustomStringConvertible</span><span>,<span> </span></span><span>CustomDebugStringConvertible</span><span><span> </span>{</span></p><div><span> </span><br></div><p><span> <span> </span></span><span>typealias</span><span><span> </span>FloatLiteralType = Double</span></p><p><span> <span> </span></span><span>typealias</span><span><span> </span>IntegerLiteralType = Int</span></p><p><span> <span> </span></span><span>typealias</span><span><span> </span>BoxType = Any</span></p><div><span> </span><br></div><p><span> <span> </span></span><span>var</span><span><span> </span>value:<span> </span></span><span>BoxType</span><span><span> </span>{<span> </span></span><span>get</span><span><span> </span></span><span>set</span><span><span> </span>}</span></p><div><span> </span><br></div><p><span> <span> </span></span><span>init</span><span>()</span></p><p><span> <span> </span></span><span>init</span><span>(</span><span>_</span><span><span> </span>value:<span> </span></span><span>BoxType</span><span>)</span></p><p>}</p><p><span>extension</span><span><span> </span></span><span>Box</span><span><span> </span></span><span>where</span><span><span> </span></span><span>Self</span><span>.BoxType == Double {</span></p><div><span> </span><br></div><p><span> <span> </span></span><span>init</span><span>(</span><span>_</span><span><span> </span>value:<span> </span></span><span>Double</span><span>) {</span></p><div><span> </span><br></div><p><span> <span> </span></span><span>self</span><span>.</span><span>init</span><span>()</span></p><p><span> <span> </span></span><span>self</span><span>.</span><span>value</span><span><span> </span>= value</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> <span> </span></span><span>init</span><span>(</span><span>_</span><span><span> </span>value:<span> </span></span><span>Int</span><span>) {</span></p><p><span> <span> </span></span><span>self</span><span>.</span><span>init</span><span>()</span></p><p><span> <span> </span></span><span>self</span><span>.</span><span>value</span><span><span> </span>=<span> </span></span><span>Double</span><span>(value)</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> <span> </span></span><span>init</span><span>(</span><span>_</span><span><span> </span>value:<span> </span></span><span>UInt32</span><span>) {</span></p><p><span> <span> </span></span><span>self</span><span>.</span><span>init</span><span>()</span></p><p><span> <span> </span></span><span>self</span><span>.</span><span>value</span><span><span> </span>=<span> </span></span><span>Double</span><span>(value)</span></p><p><span> }</span></p><p><span>}</span></p><p><span><br></span></p><p><span><br></span></p><p><span>Without that last method, Swift tries to give the UInt32 to the Int version of the method which isn't safe in my eyes and I would have expected a compiler error.</span></p></div><div class="gmail_extra" style="font-family:LucidaGrande;font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px"><br><div class="gmail_quote">On Wed, Jan 6, 2016 at 6:14 PM, Félix Cloutier<span> </span><span dir="ltr"><<a href="mailto:felixcca@yahoo.ca" target="_blank">felixcca@yahoo.ca</a>></span><span> </span>wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div style="word-wrap:break-word">I don't understand. Can you show a code example?<div><div><br><span style="font-family:'Lucida Grande';font-size:12px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline!important">Félix</span></div><br><div><blockquote type="cite"><div><div><div>Le 6 janv. 2016 à 13:02:43, James Campbell via swift-evolution <<a href="mailto:swift-evolution@swift.org" target="_blank">swift-evolution@swift.org</a>> a écrit :</div><br></div></div><div><div><div><div dir="ltr">I have a overloaded method for a protocol that takes an Int or double.<div><br></div><div>I have an issue with swift calling the Int version when passing in a UInt32 . Ideally I would prefer the compiler to detect I am about to do something which will cause an overflow and throw an error.</div><div><br></div><div>I would then have to explicitly add a overloaded UInt32 method or explicitly convert it to Int like so "Int(uintVariable)".<br><div><div><br></div>--<span> </span><br><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><span style="font-size:16px;line-height:19.2px"></span><span style="font-size:12.8px"> Wizard</span><br></div><div><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a></div><div><a href="tel:%2B44%207523%20279%20698" value="+447523279698" target="_blank">+44 7523 279 698</a></div></div></div></div></div></div></div></div></div></div></div><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=iRI3beHTe3UxYAHTlV3lA38zIPfHMhyuRzgTmGKV6k419Va-2Fjfy307WOceC97TSnJ4yKykvotDhTuWdtGSmuCkzY650M4nROHpKCTmQcDZ02kRdLGOnAQDDmImpHoyttQmzoXSgvw2ZXpGlG2RNVrFaO1Bx6wy76nUr6VThD1LZzzgoOZj9bJN4quck7YFgENkJjy9ruI1FgHPMjI-2BQsWdZHLSKm-2B592tvIuGNC-2F6u4-3D" alt="" width="1" height="1" border="0" style="min-height:1px!important;width:1px!important;border-width:0px!important;margin:0px!important;padding:0px!important"><span> </span>_______________________________________________<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" target="_blank">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br></div></blockquote></div><br></div></div></blockquote></div><br><br clear="all"><div><br></div>--<span> </span><br><div><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><span style="font-size:16px;line-height:19.2px"></span><span style="font-size:12.8px"> Wizard</span><br></div><div><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a></div><div><a href="tel:%2B44%207523%20279%20698" value="+447523279698" target="_blank">+44 7523 279 698</a></div></div></div></div></div></div></div></div></blockquote></div><br></div></div></div></blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div dir="ltr"><div><span style="font-size:16px;line-height:19.2px"></span><span style="font-size:12.8px"> Wizard</span><br></div><div><a href="mailto:james@supmenow.com" target="_blank">james@supmenow.com</a></div><div>+44 7523 279 698</div></div></div></div></div></div>
</div>