This is part of a series of posts on how Swift resolves ambiguity in overloaded functions. You can find Part 1 here. In the previous article, we looked at how generics are handled.
So finally…
Now that we’ve covered how generics fit in, we can finally answer the original question – how come you get a Range
back from 1...5
by default? To recap the sample code:
// r will be of type Range<Int> let r = 1...5 // if you want a ClosedInterval<Int>, you // have to be explicit: let integer_interval: ClosedInterval = 1...5 // if you use doubles though, you don’t need // to declare the type explicitly. // floatingpoint_interval is a ClosedInterval<Double> let floatingpoint_interval = 1.0...5.0
The infix ...
function is defined three times in the standard library, with the following signatures:
func ...<T : Comparable> (start: T, end: T) -> ClosedInterval<T> func ...<Pos : ForwardIndexType> (minimum: Pos, maximum: Pos) -> Range<Pos> func ...<Pos : ForwardIndexType where Pos : Comparable> (start: Pos, end: Pos) -> Range<Pos>
The first two are pretty similar. They are both generic, and have one generic placeholder that is constrained by a single protocol. There’s nothing favouring one of the two constraints – Comparable
and ForwardIndexType
both descend from Equatable
, but neither is a descendent of the other. Left as just those two functions, you’d get an ambiguous call error.
It’s the third function above that is what makes Swift pick Range
over ClosedInterval
. In it, Pos
is constrained not just by ForwardIndexType
but also by Comparable
. Constraining by two protocols wins over one, so this is the overload that’s picked.
Well, after 4 long lead-up posts, that was a bit of an anticlimax, eh?
So let’s ask another question – why is that last function there?
One possible explanation: the version with two protocol constrains was implemented purely to break the tie, since ambiguous call errors are annoying. Creating a Range
doesn’t actually require comparable, but adding the comparable requirement has the effect of favouring ranges over intervals. If this was the desired outcome, that could be all the extra ...
is there for.
But that’s probably not why. There’s another reason why there’s an overload that requires Comparable
.
Factory Functions
A recurring pattern in the Swift standard library is to use free functions as factories to construct different types depending on the function name or arguments.
For example, the lazy
function is defined 4 times for 4 different argument types: once each for random-access, bidirectional, and forward collections; and once for sequences.1 Each one returns a different matching lazy type (e.g. LazyRandomAccessCollection
). When you call lazy
, the compiler picks the most specific match (in the order just given). When you combine this with type inference, you get the most powerful type available declared for you, all determined at compile time.
// a will be a LazyRandomAccessCollection // since arrays are random access let a = lazy([1,2,3,4]) // s will be a LazyBidirectionalCollection, // since strings can't be indexed randomly let s = lazy("hello") // r will be a LazySequence, since StrideTo // isn't a collection, just a sequence let r = lazy(stride(from: 1, to: 8, by: 2)) // there's nothing stopping you declaring these // lazy objects manually directly: let l = LazyRandomAccessCollection([1,2,3,4])
Each version of lazy
might not even do anything other than initialize and return the relevant type – it just makes declaring different lazy types more convenient than giving the relevant class name in full. All this is made possible by the overloading priorities described in this series. Most everyday users of lazy
don’t need to know any of this though – they just use the function and it does what you’d intuitively guess was the “right” thing.
In some cases, which factory function to call is controlled more directly by the caller. For example, the stride
function creates either a StrideTo
or a StrideThrough
depending on whether the named middle argument is to:
or through:
.
// x will be a StrideThrough let x = stride(from: 1, through: 10, by: 5) // y will be a StrideTo let y = stride(from: 1, to: 10, by: 5)
In the case of stride, this is the only way you can construct these objects, as their initializers are private (which means stride
can call them since it’s declared inside Swift
, but you can’t).
Using Factory Functions to Perform Validation
For Range
, the ...
operator performs two extra tasks on top of constructing the return value. First, if the input supports comparators, it can validate that the begin and end arguments aren’t inverted. If they are, you get a runtime assertion. You only get this validation when using the ...
operator. If you try and declare a range like this: Range(start: 5, end: 1)
, it’ll work.2
By the way, you’ll even get this check with String
ranges. Swift string indices aren’t random-access (because of variable-length characters ), but they are comparable. While you can’t know how many characters are between two indices, you can know that one index comes before the another.
The other purpose of the ...
function is to normalize closed ranges into half-open ranges. Range
is kind of the opposite way around to ClosedInterval
and HalfOpenInterval
. There is no closed version of Range
, ranges are always half-open. When you call ...
, it increments the second parameter to make it equivalent to a half-open range. If you type 1...5
into a playground, you’ll see what is actually created is displayed as 1..
<6
.
This is why you'll get a runtime assertion if you ever try and construct a closed range through the end index of a string, even though in theory it could be a valid thing to do:
let s = "hello" let start = s.startIndex let end = s.endIndex // fatal error: can not increment endIndex let r = start...end // same as if you did this: end.successor()
Could you put this logic into Range.init
? In the case of the half-open conversion, maybe you could, by having two versions of init
with to:
and through:
arguments. But it's cleaner to do it using operators.
(You could maybe say the same about stride
, but then you'd need custom ternary operators.3)
But in the case of the inversion check that uses comparable, you'd have to make a generic version of init
, maybe something like this:
extension Range { init<C: Comparable>(start: C, end: C) { assert(start <= end, "Can't form Range with end < start") self.init(start: start, end: end) } }
Well, this will compile but it won't do what you want. In fact your init
will never be called, even if you pass in a comparable type. Why? Because it's generic, and the regular version of Range.init
is not. As we've seen previously, generic functions never get called when there's a possible non-generic overload.
“Hey, no wait, the current Range.init
is so generic!” you complain. “Look:”
// Excerpt from the Swift definition of Range struct Range : Equatable ...etc { /// Construct a range with `startIndex == start` and `endIndex == /// end`. init(start: T, end: T) }
“See? T is a generic placeholder! And we’re puting more constraints on our function’s placeholder, so it should be the one that’s called.”
OK yes, T
is a generic placeholder. But it’s not a placeholder for the function. It’s a placeholder for the struct. In the context of the function, T
is already fixed in place as a specific type.4 To help understand this, try the following code:
struct S<T> { func f(i: Int) { print("f(Int)") } func f(t: T) { print("f(T)") } func g(i: Int) { print("g(Int)") } // note, here scoping rules mean the placeholder // T is a _different_ T to the struct's T func g<T: IntegerType>(t: T) { print("g(T)") } } // fix T to be an Int let s = S<Int>() // error: Ambiguous use of f s.f(1) // prints "g(Int)" not "g(T)" // because generics lose... s.g(1)
Incidentally, it’s stuff like the potentially confusing scoping of T
above (or that placeholders might get randomly renamed) that makes me nervous about re-using a struct’s placeholders when extending that struct. It doesn’t feel right. Usually, nicely-written generic classes typealias their placeholders in some fashion. For example, Range
typealiases T
as Index
. Probably best to use that.
Anyway, for Range
, declaring a generic function ...
, that doesn’t have to compete with any non-generic alternatives, makes these problems go away. Plus operators look nice.
Next up: what if we didn’t want Range
to be the default? What could we do about it?
- For more on Swift’s lazy types, see this article ↩
- well, depending on your definition of “work” I guess. ↩
- If you want to see how these are possible (if perhaps inadvisable) Nate Cook recently wrote a good article about them. ↩
-
If you really want to tie your brain in knots, try to think about a secenario where the struct placeholder is fixed by which init is chosen, which relies on what
T
is… ↩
[…] Which function does Swift call? Part 5: Range vs Interval […]
[…] of posts on how Swift resolves ambiguity in overloaded functions. You can find Part 1 here. In the previous article, we looked at why you get a Range from the … by default. The answer? Because Range has some extra […]