<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 hijack this thread to talk about what the TI Voyage 200 could do as far as "type-safe calculations" go.<div class=""><br class=""></div><div class="">The calculator supports symbolic equations (for instance, `a * 1000 / 2` results in `500a` when a isn't known). The interesting part is that you could append units to numbers. For instance, you can write `500_m`, and this means 500 meters. Numbers with a type can only be added/subtracted with numbers of the same type, but they can be multiplied or divided by pretty much anything. For instance, `500_m - 2_s` (500 meters minus 2 seconds) is an error, but `500_m / 2_s` is `250 (_m/_s)` (or _m * _s^-1).</div><div class=""><br class=""></div><div class="">I found this *extremely* useful for engineering calculations. For instance, when you multiply quantities that should end up in Teslas, you know that you've done something wrong if the unit displayed after the number doesn't look like `V * s * m^-2`. It was also a lifesaver that you could do something like `6_ft` and end up with 1.8288_m (because _ft is defined as 0.3048_m).</div><div class=""><br class=""></div><div class="">I have no idea how you'd implement that with Swift though. I'm not a very powerful template wizard, but I have no idea how you'd do it with C++ either.</div><div class=""><br class=""></div><div class="">Of course, it might be a few years before you're allowed to use the Swift compiler during your physics exams, and I don't think that real-world programs often need that much unit safety with numbers. But when I read "epic typesafe calculations", that's what I think about.</div><div 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 7 janv. 2016 à 01:42:07, Thorsten Seitz via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> a écrit :</div><br class="Apple-interchange-newline"><div class=""><div class=""><div class="">I think the name should be changed to NumberBox or something similar. A box is something very generic and should not have number related associated types.<br class=""></div><div class=""><br data-mce-bogus="1" class=""></div><div class="">-Thorsten<br data-mce-bogus="1" class=""></div><div class=""><br class="">Am 06. Januar 2016 um 18:15 schrieb James Campbell <<a href="mailto:james@supmenow.com" class="">james@supmenow.com</a>>:<br class=""><br class=""><div class=""><blockquote type="cite" class=""><div class="msg-quote"><div dir="ltr" class="">I've managed to implement this already in the language with a few ugly corners due to the lack of generic protocols.<div class=""><br class=""></div><div class="">I created a protocol based on Box (<a href="https://github.com/robrix/Box/" data-mce-href="https://github.com/robrix/Box/" class="">https://github.com/robrix/Box/</a>) which works really well. I have extended this to handle certain special protocols like Equatable so you can do SpecialType == SpecialType, and even literalConversion.</div><div class=""><br class=""></div><div class="">There is however a lot of boilerplate:</div><div class=""><br class=""></div><div class="">- You have to declare all of your Convertible protocols for converting from one type to another</div><div class="">- You have to define an empty init so the protocol extensions have something to chain to.</div><div class="">- You need to write the value property with type.</div><div class=""><br class=""></div><div class="">Due to the lack of protocol generics, you also need to have a protocol for every type you wish to box which sets the associated type. Of course I could have done this with classes but I wanted to keep this as a value type :).</div><div class=""><br class=""></div><div class="">With member-wise initializations and generic protocols this could be achievable just by adding a Box protocol to the standard library.</div><div class=""><br class=""></div><div class="">Here is my implementation of Box as a protocol:</div><div class=""><br class=""></div><div class=""><p class=""><b class=""><span class="">protocol</span><span class=""> Box: </span><span class="">CustomStringConvertible</span><span class="">, </span><span class="">CustomDebugStringConvertible</span><span class=""> {</span></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">typealias</span><span class=""> FloatLiteralType = Double</span></b></p><p class=""><b class=""><span class=""> </span><span class="">typealias</span><span class=""> IntegerLiteralType = Int</span></b></p><p class=""><b class=""><span class=""> </span><span class="">typealias</span><span class=""> BoxType = Any</span></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">init</span><span class="">()</span></b></p><p class=""><b 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></b></p><p class=""><span class=""><b class="">}</b></span></p><p class=""><b class=""><span class=""></span><br class=""></b></p><p class=""><b class=""><span class="">extension</span><span class=""> </span><span class="">Box</span><span class=""> </span><span class="">where</span><span class=""> BoxType: CustomStringConvertible {</span></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">var</span><span class=""> description: </span><span class="">String</span><span class=""> {</span></b></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">var</span><span class=""> debugDescription: </span><span class="">String</span><span class=""> {</span></b></p><p class=""><b 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><span class="">㎭</span><span class="">"</span></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class="">}</b></span></p><p class=""><b class=""><span class=""></span><br class=""></b></p><p class=""><span class=""><b class="">//MARK: FloatingPointBox</b></span></p><p class=""><b class=""><span class=""></span><br class=""></b></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">typealias</span><span class=""> BoxType = Double</span></b></p><p class=""><b class=""><span class=""> </span><span class="">typealias</span><span class=""> FloatLiteralConvertible = Double</span></b></p><p class=""><b class=""><span class=""> </span><span class="">typealias</span><span class=""> IntegerLiteralConvertible = Int</span></b></p><p class=""><span class=""><b class="">}</b></span></p><p class=""><b class=""><span class=""></span><br class=""></b></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">()</span></b></p><p class=""><b class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">value</span><span class=""> = value</span></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">()</span></b></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class="">}</b></span></p><p class=""><b class=""><span class=""></span><br class=""></b></p><p class=""><b class=""><span class="">extension</span><span class=""> </span><span class="">FloatLiteralType</span><span class=""> {</span></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b 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></b></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b 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></b></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class="">}</b></span></p><p class=""><b class=""><span class=""></span><br class=""></b></p><p class=""><b class=""><span class="">extension</span><span class=""> </span><span class="">CGFloat</span><span class=""> {</span></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b 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></b></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b 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></b></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class="">}</b></span></p><p class=""><b class=""><span class=""></span><br class=""></b></p><p class=""><span class=""><b class="">//Adding FloatLiteralConvertible, IntegerLiteralConvertible</b></span></p><p class=""><b class=""><span class=""></span><br class=""></b></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">init</span><span class="">(floatLiteral value: </span><span class="">Double</span><span class="">) {</span></b></p><p class=""><b class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(value)</span></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">init</span><span class="">(integerLiteral value: </span><span class="">Int</span><span class="">) {</span></b></p><p class=""><b class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(value)</span></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b 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></b></p><p class=""><b class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(value)</span></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class="">}</b></span></p></div><div class=""><br class=""></div><div class="">Here is my example of using the Box protocol:</div><div class=""><p class=""><b class=""><span class="">struct</span><span class=""> Degree: </span><span class="">FloatingPointBox</span><span class=""> {</span></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">var</span><span class=""> value: </span><span class="">Double</span><span class=""> = </span><span class="">0</span></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">init</span><span class="">()</span></b></p><p class=""><span class=""><b class=""> {</b></span></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class="">}</b></span></p><p class=""><b class=""><span class=""></span><br class=""></b></p><p class=""><b class=""><span class="">protocol</span><span class=""> DegreeConvertiable {</span></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">init</span><span class="">(degreeLiteral value: </span><span class="">Degree</span><span class="">)</span></b></p><p class=""><span class=""><b class="">}</b></span></p><p class=""><b class=""><span class=""></span><br class=""></b></p><p class=""><b class=""><span class="">extension</span><span class=""> </span><span class="">Degree</span><span class="">: </span><span class="">RadianConvertiable</span><span class=""> {</span></b></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b class=""><span class=""> </span><span class="">init</span><span class="">(radianLiteral value: </span><span class="">Radian</span><span class="">) {</span></b></p><p class=""><b 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></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class=""> </b></span></p><p class=""><b 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></b></p><p class=""><b class=""><span class=""> </span><span class="">self</span><span class="">.</span><span class="">init</span><span class="">(radianLiteral: value)</span></b></p><p class=""><span class=""><b class=""> }</b></span></p><p class=""><span class=""><b class="">}</b></span></p></div></div><div class="gmail_extra"><br class=""><div class="gmail_quote">On Tue, Jan 5, 2016 at 5:24 PM, Matthew Johnson via swift-evolution <span dir="ltr" class=""><<a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>></span> wrote:<br class=""><blockquote class="gmail_quote" style="margin: 0 0 0 0.8ex; border-left: 1px #ccc solid; padding-left: 1ex;" data-mce-style="margin: 0 0 0 0.8ex; border-left: 1px #ccc solid; padding-left: 1ex;"><span class=""><br class=""> > On Jan 5, 2016, at 11:16 AM, Thorsten Seitz via swift-evolution <<a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:<br class=""> ><br class=""> ><br class=""> >> Am 05.01.2016 um 17:11 schrieb Grzegorz Adam Hankiewicz via swift-evolution <<a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>>:<br class=""> >><br class=""> >> The ideal would be for the compiler to pretend Euros or RefTablePk are different types, yet use their parent type at the binary level. This needs a specific syntax to teach the compiler which existing methods/operations are allowed on the new fake types and which aren’t. These new distinct types would *borrow* previous implementations.<br class=""> ><br class=""> > What about citing the relevant protocols in the newtype definition? This should include the ability to use my own protocols to which I have made the underlying type conform to by an extension.<br class=""> <br class=""> </span>This is how my forwarding proposal works. The newtype syntax I suggested as a possible extension looks like this:<br class=""> <br class=""> newtype Euro = Double forwarding Addable, Subtractable<br class=""> <br class=""> The keyword could be different, but I think `forwarding` is not bad. When I complete the second draft I think it will make even more sense. The forwarding facility has features to handle non-trivial cases (Self and associated type requirements, etc).<br class=""><div class="HOEnZb"><div class="h5"><br class=""> ><br class=""> > Throwing some syntax into the discussion:<br class=""> ><br class=""> > newtype Euro = Double : Addable, Subtractable<br class=""> ><br class=""> > where I have defined the protocols Addable and Subtractable somewhere and made Double conform to them if all this is not provided by the standard library.<br class=""> > The implementation of Euro then borrows the implementation of Double for these protocols.<br class=""> ><br class=""> > -Thorsten<br class=""> > _______________________________________________<br class=""> > swift-evolution mailing list<br class=""> > <a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""> > <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""> <br class=""> _______________________________________________<br class=""> swift-evolution mailing list<br class=""> <a href="mailto:swift-evolution@swift.org" data-mce-href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a><br class=""> <a href="https://lists.swift.org/mailman/listinfo/swift-evolution" rel="noreferrer" data-mce-href="https://lists.swift.org/mailman/listinfo/swift-evolution" class="">https://lists.swift.org/mailman/listinfo/swift-evolution</a><br class=""></div></div></blockquote></div><br class=""><br clear="all" class=""><div class=""><br class=""></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;" data-mce-style="font-size: 16px; line-height: 19.2px;" class=""></span><span style="font-size: 12.8px;" data-mce-style="font-size: 12.8px;" class=""> Wizard</span><br class=""></div><div class=""><a href="mailto:james@supmenow.com" data-mce-href="mailto:james@supmenow.com" class="">james@supmenow.com</a><br data-mce-bogus="1" class=""></div><div class="">+44 7523 279 698</div></div></div></div></div></div></div></div></blockquote></div></div>
<img src="https://u2002410.ct.sendgrid.net/wf/open?upn=iRI3beHTe3UxYAHTlV3lA38zIPfHMhyuRzgTmGKV6k6HNcWkb74NUSDxUueppvCu9gpojaO9hZVqMXWA70bVvWq4J1cDLLsnYCBRPcUGX7vfztZpLXDs1P5H2vE26cBhlOl4jucx-2BKWb7f86owE-2FdQfvirepNc1UTNMZyHIL34yiksqtvFmL-2FGRj3aQHa6jALWWzQiX5Kmj-2B-2FAvgvnj-2FMA-3D-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="">
</div>
_______________________________________________<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>