Files
swift-mirror/test/AutoDiff/stdlib/differentiable_protocol.swift
Richard Wei 8bc6143a4c [AutoDiff] Rename 'move(along:)' to 'move(by:)'.
Rename `move(along:)` to `move(by:)` based on the proposal feedback. The main argument for the change is that tangent vectors specify both a direction and a magnitude, whereas `along:` does not indicate that `self` is being moved by the specified magnitude.
2021-02-23 21:45:01 -05:00

46 lines
1.2 KiB
Swift

// RUN: %target-typecheck-verify-swift
import _Differentiation
// Test `Differentiable` protocol conformances.
struct FloatWrapper {
var value: Float
}
extension FloatWrapper: AdditiveArithmetic {
static var zero: Self {
FloatWrapper(value: Float.zero)
}
static func + (lhs: Self, rhs: Self) -> Self {
return FloatWrapper(value: lhs.value + rhs.value)
}
static func - (lhs: Self, rhs: Self) -> Self {
return FloatWrapper(value: lhs.value + rhs.value)
}
}
extension FloatWrapper: Differentiable {
public typealias TangentVector = Self
}
struct Wrapper<T> {
var value: T
}
extension Wrapper: Equatable where T: Equatable {}
extension Wrapper: AdditiveArithmetic where T: AdditiveArithmetic {
static var zero: Self {
Wrapper(value: T.zero)
}
static func + (lhs: Self, rhs: Self) -> Self {
return Wrapper(value: lhs.value + rhs.value)
}
static func - (lhs: Self, rhs: Self) -> Self {
return Wrapper(value: lhs.value + rhs.value)
}
}
extension Wrapper: Differentiable where T: Differentiable {
typealias TangentVector = Wrapper<T.TangentVector>
mutating func move(by offset: TangentVector) {
value.move(by: offset.value)
}
}