[swift-evolution] Proposal: floating point static min / max properties

Brent Royal-Gordon brent at architechies.com
Sat Dec 5 15:33:17 CST 2015


> extension Int: RangeDiscoverable {
> 	static let representableRange: Range<Int> = Int.min…Int.max
> }


There’s a problem with that:

	 24> Int.min...Int.max
	fatal error: Range end index has no valid successor

The problem is that Int.min … Int.max is actually represented as Int.min ..< Int.max.successor(), which is obviously not going to work.

> This would require numeric types to conform to ForwardIndexType.  Integer types already conform to RandomAccessIndexType.  Floating point types do not currently conform to ForwardIndexType but could conform not just to ForwardIndexType, but also BidirectionalIndexType.

Floats don’t currently conform to the IndexType protocols because there’s no *natural* interval for them to use. nextUp/nextDown are rarely what you want in practice, while 1 breaks down at large sizes.

However, we can fix both of these issues by using ClosedInterval instead of Range. ClosedInterval requires only that the bounds be Comparable, and doesn’t try to add 1 to the end.

-- 
Brent Royal-Gordon
Architechies



More information about the swift-evolution mailing list