<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body dir="auto"><div>Wasn't there something about "units" announced at wwdc that does exactly this?<br><br>Sent from my iPhone</div><div><br>On Aug 22, 2016, at 09:54, Nur Ismail via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a>&gt; wrote:<br><br></div><blockquote type="cite"><div><div dir="ltr"><div style="font-size:13px">Hi,</div><div style="font-size:13px"><br></div><div style="font-size:13px">I'm new to the list, but have an idea for Typed Numerics.</div><div style="font-size:13px">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"><br></div><div style="font-size:13px">So if I have (the syntax is made up, but perhaps something like this):</div><div style="font-size:13px">=====</div><div style="font-size:13px"><div>//Distance</div><div>struct fixedtype Distance : Double {</div><div>var km: …</div><div>var m: …</div><div>typealias meters: m</div><div>var feet: ...</div><div>...</div><div>}</div><div><br></div><div>//Weight</div><div>struct fixedtype Weight : Double {</div><div>var kg: …</div><div>var g: …</div><div>typealias grams : g</div><div>var pound: ...</div><div>}</div><div><br></div><div>…</div><div>var weight : Weight =&nbsp;<a href="http://5.kg/" target="_blank">5.kg</a>&nbsp;+ 5.g + 7.m</div><div>………………………………………..^ Compiler Error: Can’t add Distance to Weight...</div><div>var distance: Distance =&nbsp;<a href="http://7.km/" target="_blank">7.km</a>&nbsp;+ 12.5.m + 5.0 + 3</div><div>………………………………………………...^ Compiler Error: can’t add untyped number to Distance...</div></div><div style="font-size:13px">===</div><div style="font-size:13px"><br></div><div style="font-size:13px">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"><br></div><div style="font-size:13px">Any of these can be converted to it's raw untyped value, for example:</div><div style="font-size:13px">=====</div><div style="font-size:13px">let number : Double = distance.rawValue + 5.0 &nbsp; //This is allowed</div><div style="font-size:13px">distance += number.m &nbsp; //number is converted to m (meters)</div><div style="font-size:13px">=====</div><div style="font-size:13px"><br></div><div style="font-size:13px">From the Swift 3 Language guide, we are for example given the following example:</div><div style="font-size:13px">=====</div><div style="font-size:13px">extension Double {<br></div><div style="font-size:13px">&nbsp; var km: Double { return self * 1_000.0 }</div><div style="font-size:13px">&nbsp; var m: Double { return self }</div><div style="font-size:13px">&nbsp; var cm: Double { return self / 100.0 }</div><div style="font-size:13px">&nbsp; var mm: Double { return self / 1_000.0 }</div><div style="font-size:13px">&nbsp; var ft: Double { return self / 3.28084 }</div><div style="font-size:13px">}</div><div style="font-size:13px"><br></div><div style="font-size:13px"><div>let aMarathon =&nbsp;<a href="http://42.km/" target="_blank">42.km</a>&nbsp;+ 195.m</div><div>print("A marathon is \(aMarathon) meters long")</div><div>// Prints "A marathon is 42195.0 meters long"</div></div><div style="font-size:13px">=====</div><div style="font-size:13px"><br></div><div style="font-size:13px">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">&nbsp; &nbsp; &nbsp;let aValue =&nbsp;<a href="http://42.km/" target="_blank">42.km</a>&nbsp;+ 195.m + 17.pounds + 5.0</div><div style="font-size:13px">and then the code would still compile and run, but not work as intended.</div><div style="font-size:13px"><br></div><div style="font-size:13px">Extra reading, and inspiration for a feature like the above:</div><div style="font-size:13px">Mars Probe Lost Due to Simple Math Error<br></div><div style="font-size:13px"><a href="http://articles.latimes.com/1999/oct/01/news/mn-17288" target="_blank">http://articles.latimes.com/<wbr>1999/oct/01/news/mn-17288</a><br></div><div style="font-size:13px"><br></div><div style="font-size:13px">"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"><br></div><div style="font-size:13px">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"><br></div><div style="font-size:13px">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>
</div></blockquote><blockquote type="cite"><div><span>_______________________________________________</span><br><span>swift-evolution mailing list</span><br><span><a href="mailto:swift-evolution@swift.org">swift-evolution@swift.org</a></span><br><span><a href="https://lists.swift.org/mailman/listinfo/swift-evolution">https://lists.swift.org/mailman/listinfo/swift-evolution</a></span><br></div></blockquote></body></html>