[swift-evolution] [Idea] Typed Numerics

Daniel Duan daniel at duan.org
Mon Aug 22 15:13:13 CDT 2016

```Reposting comment from Dave A. in a previous thread:

"I'm not sure how useful this is, because it's not really a comment on
your efforts, but... some of us think we know how this problem domain
*should* be addressed, and we know that Swift doesn't yet have the
necessary language facilities (integer generic parameters) to do the job
right. The technique was described back in 1994 by Barton & Nackman in
“

We would have the ability to implement dimensional analysis library like the one in Boost today: http://www.boost.org/doc/libs/1_61_0/doc/html/boost_units.html

--
Daniel Duan

On August 22, 2016 at 11:54:13 AM, David Sweeris via swift-evolution (swift-evolution at swift.org) wrote:

Wasn't there something about "units" announced at wwdc that does exactly this?

Sent from my iPhone

On Aug 22, 2016, at 09:54, Nur Ismail via swift-evolution <swift-evolution at swift.org> wrote:

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 + 5.g + 7.m
………………………………………..^ Compiler Error: Can’t add Distance to Weight...
var distance: Distance = 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 + 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 + 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

"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
_______________________________________________
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/83d0895f/attachment.html>
```