This talk will explore inference from the perspective of protocols and generics and is based off a series of blog posts I've written(foxinswift.com) on the topic. In the first part of my talk casting number types through inference. I'll then show you struct serialization example demonstrating inferring a type through a mapping function. My last example will take you through inferring an associatedtype on a barebones promise implementation and we'll use it to in the context of making a network request. To finish things off I'll briefly speak on what's new in swift generics and some limitations of those features.
6. extension NumberConvertible {
func convert<T: NumberConvertible>() -> T {
switch self {
case let x as Float:
return T(x)
case let x as Int:
return T(x)
case let x as Double:
return T(x)
default:
fatalError("NumberConvertible convert failed!")
}
}
}
7. Cast by Inference!
let number = 5.5
let a: Float = number.convert()
let b: Int = number.convert()
let c: Double = number.convert()
let aa = number.convert() + Float(2)
let bb = number.convert() + Int(2)
let cc = number.convert() + Double(2)