Я пытаюсь написать базовую функцию интерполяции в swift3. Однако я получаю много ошибок. Это явно неправильный способ использования дженериков, но, может быть, у меня есть фундаментальное непонимание их применения?
class func interpolate<T>(from: T, to: T, progress: CGFloat) -> T
{
// Safety
assert(progress >= 0 && progress <= 1, "Invalid progress value: \(progress)")
if let from = from as? CGFloat, let to = to as? CGFloat
{
return from + (to - from) * progress // No + candidates produce the expected contextual result type 'T'
}
if let from = from as? CGPoint, let to = to as? CGPoint
{
var returnPoint = CGPoint()
returnPoint.x = from.x + (to.x-from.x) * progress
returnPoint.y = from.y + (to.y-from.y) * progress
return returnPoint // Cannot convert return expression of type 'CGPoint' to return type 'T'
}
if let from = from as? CGRect, let to = to as? CGRect
{
var returnRect = CGRect()
returnRect.origin.x = from.origin.x + (to.origin.x-from.origin.x) * progress
returnRect.origin.y = from.origin.y + (to.origin.y-from.origin.y) * progress
returnRect.size.width = from.size.width + (to.size.width-from.size.width) * progress
returnRect.size.height = from.size.height + (to.size.height-from.size.height) * progress
return returnRect // Cannot convert return expression of type 'CGRect' to return type 'T'
}
return nil // Nil is incompatible with return type 'T'
}