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

Tino Heth 2th at gmx.de
Sat Mar 19 10:46:25 CDT 2016


> Can't quite see where "Inheritance for structs / "newtype"-feature"
> enables this, however. Care to explain?
It's not required, but unless you want to implement a completely new numeric type (like fractions), it is tedious to declare all operations and conversions that are useful or required:

struct CustomDouble {
	let value: Double
}

func == (a: CustomDouble, b: CustomDouble) -> Bool {
	return abs(a.value - b.value) < 0.01
}

This type can handle the comparison, but actually, you don't want a struct that contains a double, but that is a double (and inherits all abilities of this type).
Greg Titus already did some research and made the observation that such simple container-types have no memory or performance penalty, but as a developer, you have to write many stupid functions that do nothing but forwarding operations on x to x.value…

Tino
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20160319/8e36ad98/attachment.html>


More information about the swift-evolution mailing list