It looks like 6 years ago I wasn't saying “that's a bad feature,” but ”this is how you do that in Swift as it exists today.” I probably wouldn't have supported adding a feature for it to Swift, though, because in the Swift of 6 years ago, it was purely a bit of nice syntactic sugar.
In Val, we realized that the distinction between projections and functions could help us solve the “property vs method” problem: if you need a projected semantics, you make it a property. If a result with independent lifetime is more appropriate, you make it a function/method. Then the question became how do you make a projection that has no receiver. We have freestanding functions; symmetry kind of demands that there are freestanding subscripts, like min
. The case you see in that example is interesting if you consider noncopyable types, which would have to be consumed if you used a function to select the mininum. For a case that's interesting with “normal” types, consider this:
subscript min<T: Comparable>(_ x: inout T, _ y: inout T): inout T {
if y < x { yield y } else { yield x }
}
Now you can write something like this:
min[&x, &y] += 1 // increment the lesser of x and y
Of course in Val you'd define min
as a bundle so one implementation could handle all the cases.