<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=""><div class="">This has definitely come up in the list before, maybe somebody else can provide some links to the conversations. </div><div class="">What I would love to see is a generic way of doing newtype, basically just defining a “typealias” but with the difference that the type checker treats it differently. </div><div class="">I’m not sure if it’s the right time to discuss this tough, unless if affects the ABI in a deep way.</div><div class=""><br class=""></div><div class="">Cheers</div><div class=""><br class=""></div><br class=""><div><blockquote type="cite" class=""><div class="">On 22 Aug 2016, at 15:54, Nur Ismail via swift-evolution <<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>> wrote:</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=""></body></html>