[swift-evolution] Pre-proposal: Safer Decimal Calculations

Rainer Brockerhoff rainer at brockerhoff.net
Sat Mar 19 07:08:03 CDT 2016

On 3/19/16 09:03, Tino Heth via swift-evolution wrote:
>> Just wanted to expand on the type tolerances idea with an example:
>> let a:Float±0.1 = 1234.56
>> let b:Float±0.5 = 123.456
>> let result:Float±0.25 = a + b // Error as b’s tolerance > 0.25
> That example is a good motivation for two things I would like to see in
> Swift:
> - Generic value parameters (has been proposed, but lacks traction)
> - Inheritance for structs / "newtype"-feature (should have been proposed
> weeks ago…)
> With both things available, it would be possible to implement your
> example with a slightly different syntax:
> let a: FloatNumber<tolerance: 0.1> = 1234.56

Or, for my proposal
	let cost: Decimal<2> = 2.34;

Can't quite see where "Inheritance for structs / "newtype"-feature"
enables this, however. Care to explain?

> Better support for plain simple calculations would be really cool — I
> still remember how impressed I've been the first time I saw the
> Scheme-Interpreter printing a number that filled the whole screen ;-)

Rainer Brockerhoff  <rainer at brockerhoff.net>
Belo Horizonte, Brazil
"In the affairs of others even fools are wise
In their own business even sages err."

More information about the swift-evolution mailing list