[swift-evolution] [Idea] Typed Numerics

Félix Cloutier felixcca at yahoo.ca
Mon Aug 22 10:28:35 CDT 2016


Hello,

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).

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 (as it is in C++ <https://github.com/zneak/units-cxx14>, for instance). I don't remember what happened with the newtype debate.

Félix

> Le 22 août 2016 à 07:54:59, Nur Ismail via swift-evolution <swift-evolution at swift.org> a écrit :
> 
> Hi,
> 
> I'm new to the list, but have an idea for Typed Numerics.
> 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.
> 
> So if I have (the syntax is made up, but perhaps something like this):
> =====
> //Distance
> struct fixedtype Distance : Double {
> var km: …
> var m: …
> typealias meters: m
> var feet: ...
> ...
> }
> 
> //Weight
> struct fixedtype Weight : Double {
> var kg: …
> var g: …
> typealias grams : g
> var pound: ...
> }
> 
>> var weight : Weight = 5.kg <http://5.kg/> + 5.g + 7.m
> ………………………………………..^ Compiler Error: Can’t add Distance to Weight...
> var distance: Distance = 7.km <http://7.km/> + 12.5.m + 5.0 + 3
> ………………………………………………...^ Compiler Error: can’t add untyped number to Distance...
> ===
> 
> 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.
> 
> Any of these can be converted to it's raw untyped value, for example:
> =====
> let number : Double = distance.rawValue + 5.0   //This is allowed
> distance += number.m   //number is converted to m (meters)
> =====
> 
> From the Swift 3 Language guide, we are for example given the following example:
> =====
> extension Double {
>   var km: Double { return self * 1_000.0 }
>   var m: Double { return self }
>   var cm: Double { return self / 100.0 }
>   var mm: Double { return self / 1_000.0 }
>   var ft: Double { return self / 3.28084 }
> }
> 
> let aMarathon = 42.km <http://42.km/> + 195.m
> print("A marathon is \(aMarathon) meters long")
> // Prints "A marathon is 42195.0 meters long"
> =====
> 
> 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:
>      let aValue = 42.km <http://42.km/> + 195.m + 17.pounds + 5.0
> and then the code would still compile and run, but not work as intended.
> 
> Extra reading, and inspiration for a feature like the above:
> Mars Probe Lost Due to Simple Math Error
> http://articles.latimes.com/1999/oct/01/news/mn-17288 <http://articles.latimes.com/1999/oct/01/news/mn-17288>
> 
> "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.
> 
> 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.
> 
> As a result, JPL engineers mistook acceleration readings measured in English units of pound-seconds for a metric measure of force called newton-seconds."
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160822/ad538053/attachment.html>


More information about the swift-evolution mailing list