David Cairns
David Cairns

Reputation: 603

Swift protocol similar to Equatable

I’m trying to create a protocol for types that are Lerp-able (linear-interpolatable). I’m declaring it similarly to how Equatable is defined:

protocol Lerpable {
    func lerp(from: Self, to: Self, alpha: Double) -> Self
}

Unfortunately, when I try to implement Lerpable for Double:

func lerp(from: Double, to: Double, alpha: Double) -> Double {
    return from + alpha * (to - from)
}
extension Double: Lerpable {}

I get an error: Type 'Double' does not conform to protocol 'Lerpable'.

I assumed this would be pretty straightforward, but maybe I just don’t understand how Equatable works. Or is it a special case in Swift? Any thoughts?


UPDATE: The correct answer is below, and here’s the final version of the code, for others’ reference:

protocol Lerpable {
    func lerp(to: Self, alpha: Double) -> Self
}

extension Double: Lerpable {
    func lerp(to: Double, alpha: Double) -> Double {
        return self + alpha * (to - self)
    }
}

func lerp<T: Lerpable>(from: T, to: T, alpha: Double) -> T {
    return from.lerp(to, alpha: alpha)
}

I added the global lerp function so I could still refer to it as
lerp(foo, bar, alpha: 0.5)
rather than
foo.lerp(bar, alpha: 0.5)

Upvotes: 4

Views: 693

Answers (2)

philtre
philtre

Reputation: 243

You may further abstract the implementation by adding a typealias for the progress value 't'

public protocol Lerpable {
    typealias LerpProgressType
    func lerpTo(value: Self, t: LerpProgressType) -> Self
}
public func lerp<T:Lerpable>(from: T, _ to: T, _ t: T.LerpProgressType) -> T {
    return from.lerpTo(to, t: t)
}

// implementations
extension Double : Lerpable {
    public typealias LerpProgressType = Double
    public func lerpTo(value: Double, t: Double) -> Double {
        return (1.0 - t) * self + t * value
    }
}
extension Float : Lerpable {
    public typealias LerpProgressType = Float
    public func lerpTo(value: Float, t: Float) -> Float {
        return (1.0 - t) * self + t * value
    }
}
extension CGFloat : Lerpable {
    public typealias LerpProgressType = CGFloat
    public func lerpTo(value: CGFloat, t: CGFloat) -> CGFloat {
        return (1.0 - t) * self + t * value
    }
}

You can now also extend various structs (such as CGPoint, CGSize, CGRect, CATransform3D, etc.):

extension CGPoint : Lerpable {
    public typealias LerpProgressType = CGFloat
    public func lerpTo(value: CGPoint, t: CGFloat) -> CGPoint {
        return
            CGPoint(
                x: x.lerpTo(value.x, t),
                y: y.lerpTo(value.y, t)
            )
    }
}
extension CLLocationCoordinate2D : LinearInterpolation {
    public typealias LerpProgressType = CLLocationDegrees
    public func lerpTo(value: CLLocationCoordinate2D, t: CLLocationDegrees) -> CLLocationCoordinate2D {
        return
            CLLocationCoordinate2D(
                latitude:  latitude.lerpTo(value.latitude, t),
                longitude: longitude.lerpTo(value.longitude, t)
            )
    }
}

To implement linear interpolation in generic structs:

public struct ValueRange<T> {
    public var start:T
    public var end:T
}

extension ValueRange where T:Lerpable {
    public func lerp(t: T.LerpProgressType) -> T {
        return start.lerpTo(end, t: t)
    }
}

Upvotes: 0

ABakerSmith
ABakerSmith

Reputation: 22939

To fix your problem you need to put the lerp function inside your extension, like so:

extension Double: Lerpable {
    func lerp(from: Double, to: Double, alpha: Double) -> Double {
        return from + alpha * (to - from)
    }
}

If you have a look at the Equatable protocol:

protocol Equatable {
    func == (lhs: Self, rhs: Self) -> Bool
}

The reason you declare its method (== specifically) outside your type extension is because Equatable wants you to overload an operator and operators must be declared at global scope. Here's an example to clarify:

protocol MyProtocol {
    func *** (lhs: Self, rhs: Self) -> Self
}

Now to make Int adopt the protocol:

extension Int : MyProtocol {}

infix operator *** {}
func *** (lhs: Int, rhs: Int) -> Int {
    return lhs * rhs
}

Upvotes: 5

Related Questions