[swift-evolution] [Idea] Typed Numerics

Nur Ismail nur at estalea.com
Mon Aug 22 09:54:59 CDT 2016


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."
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160822/fea69a0d/attachment.html>


More information about the swift-evolution mailing list