<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="">Hello,<div class=""><br class=""></div><div class="">The idea has been explored a number of times before. In some languages that already implement it, it's called "newtype". You can see previous discussions by googling "site:lists.swift.org newtype". There have also been other ideas going around for type-safe unit calculations (there's one long thread called "Epic: typesafe calculations" that ran for a while).</div><div class=""><br class=""></div><div class="">I remember that the conclusion for type-safe unit calculations was that we didn't want to implement it as a compiler feature, but rather implement compiler features that would make it possible (<a href="https://github.com/zneak/units-cxx14" class="">as it is in C++</a>, for instance). I don't remember what happened with the newtype debate.</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 22 août 2016 à 07:54:59, Nur Ismail 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 dir="ltr" class=""><div style="font-size:13px" class="">Hi,</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">I'm new to the list, but have an idea for Typed Numerics.</div><div style="font-size:13px" class="">Basically numeric values (such as Double, Int, etc.) that are strongly typed to a specific use case, for example Distance, Weight, etc. and cannot be intermixed with untyped values.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">So if I have (the syntax is made up, but perhaps something like this):</div><div style="font-size:13px" class="">=====</div><div style="font-size:13px" class=""><div class="">//Distance</div><div class="">struct fixedtype Distance : Double {</div><div class="">var km: …</div><div class="">var m: …</div><div class="">typealias meters: m</div><div class="">var feet: ...</div><div class="">...</div><div class="">}</div><div class=""><br class=""></div><div class="">//Weight</div><div class="">struct fixedtype Weight : Double {</div><div class="">var kg: …</div><div class="">var g: …</div><div class="">typealias grams : g</div><div class="">var pound: ...</div><div class="">}</div><div class=""><br class=""></div><div class="">…</div><div class="">var weight : Weight = <a href="http://5.kg/" target="_blank" class="">5.kg</a> + 5.g + 7.m</div><div class="">………………………………………..^ Compiler Error: Can’t add Distance to Weight...</div><div class="">var distance: Distance = <a href="http://7.km/" target="_blank" class="">7.km</a> + 12.5.m + 5.0 + 3</div><div class="">………………………………………………...^ Compiler Error: can’t add untyped number to Distance...</div></div><div style="font-size:13px" class="">===</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">The main restriction this syntax should do is disallow intermixing of numeric types (even if they all descend from Double, Int, etc.) and not allow adding untyped numerics (i.e. those without a type suffix), unless explicitly asked for in the code.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">Any of these can be converted to it's raw untyped value, for example:</div><div style="font-size:13px" class="">=====</div><div style="font-size:13px" class="">let number : Double = distance.rawValue + 5.0 //This is allowed</div><div style="font-size:13px" class="">distance += number.m //number is converted to m (meters)</div><div style="font-size:13px" class="">=====</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">From the Swift 3 Language guide, we are for example given the following example:</div><div style="font-size:13px" class="">=====</div><div style="font-size:13px" class="">extension Double {<br class=""></div><div style="font-size:13px" class=""> var km: Double { return self * 1_000.0 }</div><div style="font-size:13px" class=""> var m: Double { return self }</div><div style="font-size:13px" class=""> var cm: Double { return self / 100.0 }</div><div style="font-size:13px" class=""> var mm: Double { return self / 1_000.0 }</div><div style="font-size:13px" class=""> var ft: Double { return self / 3.28084 }</div><div style="font-size:13px" class="">}</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class=""><div class="">let aMarathon = <a href="http://42.km/" target="_blank" class="">42.km</a> + 195.m</div><div class="">print("A marathon is \(aMarathon) meters long")</div><div class="">// Prints "A marathon is 42195.0 meters long"</div></div><div style="font-size:13px" class="">=====</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">This is quite nice to suffix a conversion method after the value, but if I had another extension that converts the values to pounds and kilograms, then one can illegally do this:</div><div style="font-size:13px" class=""> let aValue = <a href="http://42.km/" target="_blank" class="">42.km</a> + 195.m + 17.pounds + 5.0</div><div style="font-size:13px" class="">and then the code would still compile and run, but not work as intended.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">Extra reading, and inspiration for a feature like the above:</div><div style="font-size:13px" class="">Mars Probe Lost Due to Simple Math Error<br class=""></div><div style="font-size:13px" class=""><a href="http://articles.latimes.com/1999/oct/01/news/mn-17288" target="_blank" class="">http://articles.latimes.com/<wbr class="">1999/oct/01/news/mn-17288</a><br class=""></div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">"NASA lost its $125-million Mars Climate Orbiter because spacecraft engineers failed to convert from English to metric measurements when exchanging vital data before the craft was launched, space agency officials said Thursday.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">A navigation team at the Jet Propulsion Laboratory used the metric system of millimeters and meters in its calculations, while Lockheed Martin Astronautics in Denver, which designed and built the spacecraft, provided crucial acceleration data in the English system of inches, feet and pounds.</div><div style="font-size:13px" class=""><br class=""></div><div style="font-size:13px" class="">As a result, JPL engineers mistook acceleration readings measured in English units of pound-seconds for a metric measure of force called newton-seconds."</div></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>