[swift-evolution] [early Pitch] retype (or newtype): strong type-alias (typedef) for redoing an interface; also: object aliases

Daryle Walker darylew at mac.com
Tue Jun 20 17:37:13 CDT 2017


I didn’t add a SuperReallyPeachyKeenProtocol example. It assumes we eventually add variadic generics:

retype TupleCollection<T, U…>: (T, …U), RandomAccessCollection allwhere U == T {
export default  // Without this, instances of “self” below would be “super” instead.

var head: T {
get { return self.0 }
set { self.0 = newValue }
}
var tail: (…U) {
// Put some cool template meta-programming here, thanks.
}

subscript(index: Int) -> T {
get { return index == 0 ? self.0 : tail[index - 1] }
set {
if index == 0 {
self.0 = newValue
} else {
tail[index - 1] = newValue
}
}
}
//…
}

// Put terminal specialization of TupleCollection<> here.

//…

func myFunc2() {
var myTuple: (Int, Int, Int, Int) = (0, 0, 0, 0)
pose myCollection = myTuple as TupleCollection

for i in myCollection {
i += 2
}
print(myTuple)  // (2, 2, 2, 2)
}

Now you don’t have to permanently add subscripting to tuple types.

[Oh, instead of expanding a parameter pack with U…, I automatically expand it with “where” variants where-any and where-all. The first considers each member of the pack separately and then applies logical-OR to the result (default to FALSE when empty). The second does a logical-AND (defaulting to TRUE when empty). The where-any marker could be renamed to “anywhere,” but that would need an “allwhere” to be consistent. Turns, out “allwhere” is a word, albeit archaic!]

— 
Daryle Walker
Mac, Internet, and Video Game Junkie
darylew AT mac DOT com 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170620/12616fdb/attachment.html>


More information about the swift-evolution mailing list