<div dir="ltr">Seem like the protocol I implemented is actually a Box, so perhaps just adding a box type would allow us to do most of these things.<div><br></div><div><a href="https://github.com/robrix/Box">https://github.com/robrix/Box</a><br></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jan 6, 2016 at 4:02 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">There are lots of people who had an idea like this one, the latest just yesterday, where the other threads had been collected: <a href="https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160104/005353.html" target="_blank">https://lists.swift.org/pipermail/swift-evolution/Week-of-Mon-20160104/005353.html</a><div><br></div><div>So there's definitely a lot of interest in that.</div><div><br></div><div>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><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>
<br><div><blockquote type="cite"><div><div class="h5"><div>Le 6 janv. 2016 à 10:17:23, 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 class="h5"><div dir="ltr"><div>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><br></div><div>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><br></div><div>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. </div><div><br></div><div>So two questions do we have something like this already? and if not would this be useful ?</div><div><br></div><div><p><span>import</span><span> Foundation</span></p><p><span></span><br></p><p><span>//MARK:- AngleType</span></p><p><span></span><br></p><p><span>protocol</span><span> AngleType: </span><span>FloatLiteralConvertible</span><span>, </span><span>IntegerLiteralConvertible</span><span> {</span></p><div><span> </span><br></div><p><span> </span><span>var</span><span> value: </span><span>Double</span><span> { </span><span>get</span><span> </span><span>set</span><span> }</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span>(</span><span>_</span><span> value: </span><span>Double</span><span>)</span></p><p><span> </span><span>init</span><span>(</span><span>_</span><span> value: </span><span>Int</span><span>)</span></p><p><span> </span><span>init</span><span><T: </span><span>IntegerType</span><span>>(integerLiteral value: </span><span>T</span><span>)</span></p><p><span> </span><span>init</span><span><T: </span><span>FloatingPointType</span><span>>(floatLiteral value: </span><span>T</span><span>)</span></p><p><span>}</span></p><p><span></span><br></p><p><span>// Implement FloatLiteralConvertible and IntegerLiteralConvertible</span></p><p><span></span><br></p><p><span>extension</span><span> </span><span>AngleType</span><span> {</span></p><p><span></span><br></p><p><span> </span><span>init</span><span><T: </span><span>IntegerType</span><span>>(integerLiteral value: </span><span>T</span><span>)</span></p><p><span> {</span></p><p><span> </span><span>self</span><span>.</span><span>init</span><span>(value)</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span><T: </span><span>IntegerType</span><span>>(</span><span>_</span><span> value: </span><span>T</span><span>)</span></p><p><span> {</span></p><p><span> </span><span>self</span><span>.</span><span>init</span><span>(integerLiteral: value)</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span><T: </span><span>FloatingPointType</span><span>>(floatLiteral value: </span><span>T</span><span>)</span></p><p><span> {</span></p><p><span> </span><span>self</span><span>.</span><span>init</span><span>(value)</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span><T: </span><span>FloatingPointType</span><span>>(</span><span>_</span><span> value: </span><span>T</span><span>)</span></p><p><span> {</span></p><p><span> </span><span>self</span><span>.</span><span>init</span><span>(floatLiteral: value)</span></p><p><span> }</span></p><p><span>}</span></p><p><span></span><br></p><p><span>//MARK:- Degree</span></p><p><span></span><br></p><p><span>struct</span><span> Degree: </span><span>AngleType</span><span> {</span></p><div><span> </span><br></div><p><span> </span><span>typealias</span><span> FloatLiteralType = </span><span>Double</span></p><p><span> </span><span>typealias</span><span> IntegerLiteralType = </span><span>Int</span></p><div><span> </span><br></div><p><span> </span><span>var</span><span> value: </span><span>Double</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span>(</span><span>_</span><span> value: </span><span>Double</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>value</span><span> = value</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span>(</span><span>_</span><span> value: </span><span>Int</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>value</span><span> = </span><span>Double</span><span>(value)</span></p><p><span> }</span></p><p><span>}</span></p><p><span></span><br></p><p><span>protocol</span><span> DegreeConvertiable {</span></p><p><span> </span><span>init</span><span>(degreeLiteral value: </span><span>Degree</span><span>)</span></p><p><span>}</span></p><p><span></span><br></p><p><span>extension</span><span> </span><span>Degree</span><span>: </span><span>CustomStringConvertible</span><span>, </span><span>CustomDebugStringConvertible</span><span> {</span></p><p><span> </span><span>var</span><span> description: </span><span>String</span><span> {</span></p><p><span> </span><span>return</span><span> </span><span>self</span><span>.</span><span>value</span><span>.</span><span>description</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>var</span><span> debugDescription: </span><span>String</span><span> {</span></p><p><span> </span><span>return</span><span> </span><span>"</span><span>\</span><span>(</span><span>self</span><span>.</span><span>value</span><span>.</span><span>description</span><span>)°"</span></p><p><span> }</span></p><p><span>}</span></p><p><span></span><br></p><p><span>extension</span><span> </span><span>Degree</span><span>: </span><span>RadianConvertiable</span><span> {</span></p><p><span> </span><span>init</span><span>(radianLiteral value: </span><span>Radian</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>value</span><span> = </span><span>Double</span><span>(radianLiteral:value) * </span><span>180.0</span><span> / </span><span>M_PI</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span>(</span><span>_</span><span> value: </span><span>Radian</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>init</span><span>(radianLiteral: value)</span></p><p><span> }</span></p><p><span>}</span></p><p><span></span><br></p><p><span>//MARK:- Radian</span></p><p><span></span><br></p><p><span>struct</span><span> Radian: </span><span>AngleType</span><span> {</span></p><div><span> </span><br></div><p><span> </span><span>typealias</span><span> FloatLiteralType = </span><span>Double</span></p><p><span> </span><span>typealias</span><span> IntegerLiteralType = </span><span>Int</span></p><div><span> </span><br></div><p><span> </span><span>var</span><span> value: </span><span>Double</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span>(</span><span>_</span><span> value: </span><span>Double</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>value</span><span> = value</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span>(</span><span>_</span><span> value: </span><span>Int</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>value</span><span> = </span><span>Double</span><span>(value)</span></p><p><span> }</span></p><p><span>}</span></p><p><span></span><br></p><p><span>protocol</span><span> RadianConvertiable {</span></p><p><span> </span><span>init</span><span>(radianLiteral value: </span><span>Radian</span><span>)</span></p><p><span>}</span></p><p><span></span><br></p><p><span>extension</span><span> </span><span>Radian</span><span>: </span><span>CustomStringConvertible</span><span>, </span><span>CustomDebugStringConvertible</span><span> {</span></p><p><span> </span><span>var</span><span> description: </span><span>String</span><span> {</span></p><p><span> </span><span>return</span><span> </span><span>self</span><span>.</span><span>value</span><span>.</span><span>description</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>var</span><span> debugDescription: </span><span>String</span><span> {</span></p><p><span> </span><span>return</span><span> </span><span>"</span><span>\</span><span>(</span><span>self</span><span>.</span><span>value</span><span>.</span><span>description</span><span>)</span><span>㎭</span><span>"</span></p><p><span> }</span></p><p><span>}</span></p><p><span></span><br></p><p><span>extension</span><span> </span><span>Radian</span><span>: </span><span>DegreeConvertiable</span><span> {</span></p><p><span> </span><span>init</span><span>(degreeLiteral value: </span><span>Degree</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>value</span><span> = </span><span>Double</span><span>(degreeLiteral: value) * </span><span>M_PI</span><span> / </span><span>180.0</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span>(</span><span>_</span><span> value: </span><span>Degree</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>init</span><span>(degreeLiteral: value)</span></p><p><span> }</span></p><p><span>}</span></p><p><span></span><br></p><p><span>//MARK:- Adding Conformance To Built In Types</span></p><p><span></span><br></p><p><span>extension</span><span> </span><span>FloatLiteralType</span><span>: </span><span>DegreeConvertiable</span><span>, </span><span>RadianConvertiable</span></p><p><span>{</span></p><p><span> </span><span>init</span><span>(degreeLiteral degree: </span><span>Degree</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>value</span><span> = degree.</span><span>value</span><span>.</span><span>value</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span>(radianLiteral radian: </span><span>Radian</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>value</span><span> = radian.</span><span>value</span><span>.</span><span>value</span></p><p><span> }</span></p><p><span>}</span></p><p><span></span><br></p><p><span>extension</span><span> </span><span>CGFloat</span><span>: </span><span>DegreeConvertiable</span><span>, </span><span>RadianConvertiable</span></p><p><span>{</span></p><p><span> </span><span>init</span><span>(degreeLiteral degree: </span><span>Degree</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>init</span><span>(degree.</span><span>value</span><span>)</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span>(radianLiteral radian: </span><span>Radian</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>init</span><span>(radian.</span><span>value</span><span>)</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span>(</span><span>_</span><span> degree: </span><span>Degree</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>init</span><span>(degreeLiteral: degree)</span></p><p><span> }</span></p><div><span> </span><br></div><p><span> </span><span>init</span><span>(</span><span>_</span><span> radian: </span><span>Radian</span><span>) {</span></p><p><span> </span><span>self</span><span>.</span><span>init</span><span>(radianLiteral: radian)</span></p><p><span> }</span></p><p><span>}</span></p></div>-- <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><img src="https://u2002410.ct.sendgrid.net/wf/open?upn=iRI3beHTe3UxYAHTlV3lA38zIPfHMhyuRzgTmGKV6k6iS-2B3F7sOIVaOZCo1uQ3xned0LaZRj5-2Fh6Q4OPMoWArR-2BlBxShhlH1DcgPCgyHg1FfQS6z5m1OOv2Nr9pOkqJWRUJt57mkiLk4sLxYkfBozzvX5fldrwvPONsMxfIO0TAdoRBILTtDowGue4XsaapfeGftYXX5sf2miytQqN4Kc3JmNexW-2Bfrl711jfMyADvA-3D" alt="" width="1" height="1" border="0" style="min-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">
_______________________________________________<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>-- <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>