<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="">There are lots of people who had an idea like this one, the latest just yesterday, where the other threads had been collected:&nbsp;<a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160104/005353.html" class="">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160104/005353.html</a><div class=""><br class=""></div><div class="">So there's definitely a lot of interest in that.</div><div class=""><br class=""></div><div class="">Freshly out of engineering school, I really liked how my calculator handled that (a bulky old TI Voyage 200, though I think that the N-Spire line can do it too).<br class=""><div class="">
<br class="Apple-interchange-newline"><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; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; display: inline !important; float: none;" class="">Félix</span>
</div>

<br class=""><div><blockquote type="cite" class=""><div class="">Le 6 janv. 2016 à 10:17:23, James Campbell via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; a écrit :</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="">I implemented a way of type safeing a calculation in Swift. I.e marking an Int as a Degree and then being able to convert to a Radian. (At the bottom of the page)</div><div class=""><br class=""></div><div class="">The magic for these special structs was the AngleType protocol which allows you to Box up a value (in this case a number) and it handles the Conversion (Apart from two of the methods which I had to implement).</div><div class=""><br class=""></div><div class="">In theory we could abstract this protocol out, so that others could use it in the library. Then they just use it with their structs and they can handle converting between these types.&nbsp;</div><div class=""><br class=""></div><div class="">So two questions do we have something like this already? and if not would this be useful ?</div><div class=""><br class=""></div><div class=""><p class=""><span class="">import</span><span class=""> Foundation</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">//MARK:- AngleType</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">protocol</span><span class=""> AngleType: </span><span class="">FloatLiteralConvertible</span><span class="">, </span><span class="">IntegerLiteralConvertible</span><span class=""> {</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">var</span><span class=""> value: </span><span class="">Double</span><span class=""> { </span><span class="">get</span><span class=""> </span><span class="">set</span><span class=""> }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </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="">&nbsp; &nbsp; </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="">&nbsp; &nbsp; </span><span class="">init</span><span class="">&lt;T: </span><span class="">IntegerType</span><span class="">&gt;(integerLiteral value: </span><span class="">T</span><span class="">)</span></p><p class=""><span class="">&nbsp; &nbsp; </span><span class="">init</span><span class="">&lt;T: </span><span class="">FloatingPointType</span><span class="">&gt;(floatLiteral value: </span><span class="">T</span><span class="">)</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">// Implement FloatLiteralConvertible and IntegerLiteralConvertible</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">extension</span><span class=""> </span><span class="">AngleType</span><span class=""> {</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">&nbsp; &nbsp; </span><span class="">init</span><span class="">&lt;T: </span><span class="">IntegerType</span><span class="">&gt;(integerLiteral value: </span><span class="">T</span><span class="">)</span></p><p class=""><span class="">&nbsp; &nbsp; {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(value)</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">init</span><span class="">&lt;T: </span><span class="">IntegerType</span><span class="">&gt;(</span><span class="">_</span><span class=""> value: </span><span class="">T</span><span class="">)</span></p><p class=""><span class="">&nbsp; &nbsp; {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(integerLiteral: value)</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">init</span><span class="">&lt;T: </span><span class="">FloatingPointType</span><span class="">&gt;(floatLiteral value: </span><span class="">T</span><span class="">)</span></p><p class=""><span class="">&nbsp; &nbsp; {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(value)</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">init</span><span class="">&lt;T: </span><span class="">FloatingPointType</span><span class="">&gt;(</span><span class="">_</span><span class=""> value: </span><span class="">T</span><span class="">)</span></p><p class=""><span class="">&nbsp; &nbsp; {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(floatLiteral: value)</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">//MARK:- Degree</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">struct</span><span class=""> Degree: </span><span class="">AngleType</span><span class=""> {</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">typealias</span><span class=""> FloatLiteralType = </span><span class="">Double</span></p><p class=""><span class="">&nbsp; &nbsp; </span><span class="">typealias</span><span class=""> IntegerLiteralType = </span><span class="">Int</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">var</span><span class=""> value: </span><span class="">Double</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </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="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">value</span><span class=""> = value</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </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="">&nbsp; &nbsp; &nbsp; &nbsp; </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="">&nbsp; &nbsp; }</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">protocol</span><span class=""> DegreeConvertiable {</span></p><p class=""><span class="">&nbsp; &nbsp; </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 class=""></p><p class=""><span class="">extension</span><span class=""> </span><span class="">Degree</span><span class="">: </span><span class="">CustomStringConvertible</span><span class="">, </span><span class="">CustomDebugStringConvertible</span><span class=""> {</span></p><p class=""><span class="">&nbsp; &nbsp; </span><span class="">var</span><span class=""> description: </span><span class="">String</span><span class=""> {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </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="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">var</span><span class=""> debugDescription: </span><span class="">String</span><span class=""> {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </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="">&nbsp; &nbsp; }</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br class=""></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="">&nbsp; &nbsp; </span><span class="">init</span><span class="">(radianLiteral value: </span><span class="">Radian</span><span class="">) {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">value</span><span class=""> = </span><span class="">Double</span><span class="">(radianLiteral:value) * </span><span class="">180.0</span><span class=""> / </span><span class="">M_PI</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </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="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(radianLiteral: value)</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">//MARK:- Radian</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">struct</span><span class=""> Radian: </span><span class="">AngleType</span><span class=""> {</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">typealias</span><span class=""> FloatLiteralType = </span><span class="">Double</span></p><p class=""><span class="">&nbsp; &nbsp; </span><span class="">typealias</span><span class=""> IntegerLiteralType = </span><span class="">Int</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">var</span><span class=""> value: </span><span class="">Double</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </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="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">value</span><span class=""> = value</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </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="">&nbsp; &nbsp; &nbsp; &nbsp; </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="">&nbsp; &nbsp; }</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">protocol</span><span class=""> RadianConvertiable {</span></p><p class=""><span class="">&nbsp; &nbsp; </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 class=""></p><p class=""><span class="">extension</span><span class=""> </span><span class="">Radian</span><span class="">: </span><span class="">CustomStringConvertible</span><span class="">, </span><span class="">CustomDebugStringConvertible</span><span class=""> {</span></p><p class=""><span class="">&nbsp; &nbsp; </span><span class="">var</span><span class=""> description: </span><span class="">String</span><span class=""> {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </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="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">var</span><span class=""> debugDescription: </span><span class="">String</span><span class=""> {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </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><span class="">㎭</span><span class="">"</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br class=""></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="">&nbsp; &nbsp; </span><span class="">init</span><span class="">(degreeLiteral value: </span><span class="">Degree</span><span class="">) {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">value</span><span class=""> = </span><span class="">Double</span><span class="">(degreeLiteral: value) * </span><span class="">M_PI</span><span class=""> / </span><span class="">180.0</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </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="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(degreeLiteral: value)</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">//MARK:- Adding Conformance To Built In Types</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">extension</span><span class=""> </span><span class="">FloatLiteralType</span><span class="">: </span><span class="">DegreeConvertiable</span><span class="">, </span><span class="">RadianConvertiable</span></p><p class=""><span class="">{</span></p><p class=""><span class="">&nbsp; &nbsp; </span><span class="">init</span><span class="">(degreeLiteral degree: </span><span class="">Degree</span><span class="">) {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">value</span><span class=""> = degree.</span><span class="">value</span><span class="">.</span><span class="">value</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">init</span><span class="">(radianLiteral radian: </span><span class="">Radian</span><span class="">) {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">value</span><span class=""> = radian.</span><span class="">value</span><span class="">.</span><span class="">value</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><p class=""><span class="">}</span></p><p class=""><span class=""></span><br class=""></p><p class=""><span class="">extension</span><span class=""> </span><span class="">CGFloat</span><span class="">: </span><span class="">DegreeConvertiable</span><span class="">, </span><span class="">RadianConvertiable</span></p><p class=""><span class="">{</span></p><p class=""><span class="">&nbsp; &nbsp; </span><span class="">init</span><span class="">(degreeLiteral degree: </span><span class="">Degree</span><span class="">) {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(degree.</span><span class="">value</span><span class="">)</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">init</span><span class="">(radianLiteral radian: </span><span class="">Radian</span><span class="">) {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(radian.</span><span class="">value</span><span class="">)</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">init</span><span class="">(</span><span class="">_</span><span class=""> degree: </span><span class="">Degree</span><span class="">) {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(degreeLiteral: degree)</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><div class=""><span class="">&nbsp;&nbsp; &nbsp;</span><br class="webkit-block-placeholder"></div><p class=""><span class="">&nbsp; &nbsp; </span><span class="">init</span><span class="">(</span><span class="">_</span><span class=""> radian: </span><span class="">Radian</span><span class="">) {</span></p><p class=""><span class="">&nbsp; &nbsp; &nbsp; &nbsp; </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(radianLiteral: radian)</span></p><p class=""><span class="">&nbsp; &nbsp; }</span></p><p class=""><span class="">}</span></p></div>-- <br class=""><div class="gmail_signature"><div dir="ltr" class=""><div class=""><div dir="ltr" class=""><div dir="ltr" class=""><div class=""><span style="font-size:16px;line-height:19.2px" class=""></span><span style="font-size:12.8px" class="">&nbsp;Wizard</span><br class=""></div><div class=""><a href="mailto:james@supmenow.com" target="_blank" class="">james@supmenow.com</a></div><div class="">+44 7523 279 698</div></div></div></div></div></div>
</div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=iRI3beHTe3UxYAHTlV3lA38zIPfHMhyuRzgTmGKV6k6iS-2B3F7sOIVaOZCo1uQ3xned0LaZRj5-2Fh6Q4OPMoWArR-2BlBxShhlH1DcgPCgyHg1FfQS6z5m1OOv2Nr9pOkqJWRUJt57mkiLk4sLxYkfBozzvX5fldrwvPONsMxfIO0TAdoRBILTtDowGue4XsaapfeGftYXX5sf2miytQqN4Kc3JmNexW-2Bfrl711jfMyADvA-3D" alt="" width="1" height="1" border="0" style="height:1px !important;width:1px !important;border-width:0 !important;margin-top:0 !important;margin-bottom:0 !important;margin-right:0 !important;margin-left:0 !important;padding-top:0 !important;padding-bottom:0 !important;padding-right:0 !important;padding-left:0 !important;" class="">
_______________________________________________<br class="">swift-evolution mailing list<br class=""><a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class="">https://lists.swift.org/mailman/listinfo/swift-evolution<br class=""></div></blockquote></div><br class=""></div></body></html>