Created
November 25, 2015 15:35
-
-
Save jepers/956571693c3d5d5b9a99 to your computer and use it in GitHub Desktop.
This file contains 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
//---------------------------------------------------------------------------------------------------- | |
// This program compiles and runs. Question: Can generic struct X be made to satisfy TEST? | |
//---------------------------------------------------------------------------------------------------- | |
protocol DefaultInitializable { init() } | |
extension Double : DefaultInitializable {} | |
extension Float : DefaultInitializable {} | |
extension Int : DefaultInitializable {} | |
extension UInt8 : DefaultInitializable {} | |
// ... | |
protocol CountType { static var value: Int { get } } | |
struct Count0 : CountType { static var value: Int { return 0 } } | |
struct CountSuccessorOf<T: CountType> : CountType { static var value: Int { return T.value + 1 } } | |
extension CountType { | |
typealias Plus1 = CountSuccessorOf<Self> | |
typealias Plus2 = Plus1.Plus1 | |
typealias Plus3 = Plus1.Plus2 | |
typealias Plus4 = Plus1.Plus3 | |
// ... | |
} | |
typealias Count1 = Count0.Plus1 | |
typealias Count2 = Count0.Plus2 | |
typealias Count3 = Count0.Plus3 | |
typealias Count4 = Count0.Plus4 | |
// ... | |
//---------------------------------------------------------------------------------------------------- | |
// X (HOWTODO) | |
//---------------------------------------------------------------------------------------------------- | |
struct X<C: CountType, E: DefaultInitializable> { // X must be parameterized by Count and Element. | |
typealias Count = C | |
typealias Element = E | |
//eg var storage: SomethingThatWillMakeXHaveTheSizeOfCountElements | |
} | |
//---------------------------------------------------------------------------------------------------- | |
// TEST | |
//---------------------------------------------------------------------------------------------------- | |
typealias SomeCount = Count4 // Or Count3, or Count0, or Count4.Plus4.Plus4, ... | |
typealias SomeElement = Double // Or Int, or Float, or UInt8, ... | |
typealias SomeX = X<SomeCount, SomeElement> | |
let currentStride = strideof(SomeX) | |
let correctStride = SomeCount.value * strideof(SomeElement) | |
print("Current stride:", currentStride) | |
print("Correct stride:", correctStride) | |
if currentStride == correctStride { | |
print("X works for Element \(SomeElement.self) and Count \(SomeCount.value)") | |
} else { | |
print("X is not working ...") | |
} | |
//---------------------------------------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment