Created
March 13, 2019 14:33
-
-
Save SaganRitual/f21011da0cc490563c7d8b4133b2e51c to your computer and use it in GitHub Desktop.
I'm not a bad person. Why must the universe punish me so with Swift generics?
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
struct ProtoOrder<T> { | |
var quantity: T | |
init(_ quantity: T) { self.quantity = quantity } | |
} | |
protocol ItemProtocol { | |
associatedtype HowCounted: Numeric | |
typealias Order = ProtoOrder<HowCounted> | |
var order: Order { get set } | |
mutating func deliver() -> Bool | |
} | |
extension ItemProtocol { | |
mutating func deliver() -> Bool { | |
return Utilities.deliver(order) | |
} | |
} | |
struct Shovels: ItemProtocol { | |
typealias HowCounted = Int | |
var order: Order | |
} | |
struct Gravel: ItemProtocol { | |
typealias HowCounted = Double | |
var order: Order | |
} | |
enum Utilities { | |
static func deliver<T>(_ order: ProtoOrder<T>) -> Bool where T == Int { | |
return true | |
} | |
} |
Or this
struct ProtoOrder<T> {
var quantity: T
init(_ quantity: T) { self.quantity = quantity }
}
protocol ItemProtocol {
associatedtype HowCounted: Numeric
typealias Order = ProtoOrder<HowCounted>
var order: Order { get set }
mutating func deliver() -> Bool
}
extension ItemProtocol {
mutating func deliver() -> Bool {
return Utilities.deliver(order)
}
}
struct Shovels: ItemProtocol {
typealias HowCounted = Int
var order: Order
}
struct Gravel: ItemProtocol {
typealias HowCounted = Double
var order: Order
}
enum Utilities {
static func deliver<T: Numeric>(_ order: ProtoOrder<T>) -> Bool {
return true
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you try something like this?