Operator member syntax

Context: operator member syntax popped up in a discussion regarding the @derivative(of:) and @transpose(of:) attributes for differentiable programming.

These attributes reference a declaration, which may be an operator:

@derivative(of: Float.+)
func addDerivative(lhs: Float, rhs: Float) -> (value: Float, differential: (Float, Float) -> Float) { … }

In practice, qualified operator member syntax isn't necessary for these attributes. Type-checking for @derivative(of:) and @transpose(of:) currently require the operator to be defined in the same type context, where lookup is not ambiguous:

extension Float {
    @derivative(of: +(_:_:))
    static func addDerivative(lhs: Float, rhs: Float) -> (value: Float, differential: (Float, Float) -> Float) {
        (lhs + rhs, { $0 + $1 })
    }
}

But this motivated the discussion nonetheless!

1 Like