Created
November 11, 2015 20:57
-
-
Save jepers/c35f28b546eddc13761d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//------------------------------------------------------------------------------ | |
// The following compiles and works, but see comments for 2 compiler crashers. | |
//------------------------------------------------------------------------------ | |
protocol DefaultInitializable { init() } | |
extension Int: DefaultInitializable {} | |
// Hople = Homogeneous tuple / static array implemented using recursive structs. | |
protocol HopleType : DefaultInitializable { | |
typealias Element: DefaultInitializable | |
subscript(index: Int) -> Element { get set } | |
} | |
struct EmptyHople<Element : DefaultInitializable> : HopleType { | |
init() {} | |
subscript(i: Int) -> Element { // <--- Replacing i with _ crashes compiler. | |
get { fatalError("Index out of bounds!") } | |
set { fatalError("Index out of bounds!") } | |
} | |
} | |
struct HopleAfter<Tail : HopleType> : HopleType { | |
var head: Tail.Element | |
var tail: Tail | |
init() { (head, tail) = (Tail.Element(), Tail()) } | |
subscript(i: Int) -> Tail.Element { | |
get { if i == 0 { return head } else { return tail[i - 1] } } | |
set { if i == 0 { head = newValue } else { tail[i - 1] = newValue } } | |
} | |
} | |
struct HopleOf<Element: DefaultInitializable> { | |
typealias Count0 = EmptyHople<Element> | |
typealias Count1 = HopleAfter<Count0> | |
typealias Count2 = HopleAfter<Count1> | |
typealias Count3 = HopleAfter<Count2> | |
} | |
var a = HopleOf<Int>.Count3() // <--- Replacing var with let crashes compiler. | |
a[0] = 1 | |
a[1] = 2 | |
a[2] = 3 | |
print(strideofValue(a)) // 24 (ie stride of three Ints, 3 * 8) | |
print(a[0]) // 1 | |
print(a[1]) // 2 | |
print(a[2]) // 3 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment