Skip to content

Conversation

@dan-zheng
Copy link
Contributor

The Differentiable.zeroTangentVectorInitializer protocol requirement is a closure producing a zero tangent vector, capturing minimal information from self.

Fixes the Complex: Differentiable conformance for master toolchains:

$ swift --version
Apple Swift version 5.3-dev (LLVM f516ac602c, Swift 09ea5fd1f7)
Target: x86_64-apple-darwin19.3.0

$ swift build
swift-numerics/Sources/ComplexModule/Differentiable.swift:15:1: error: implementation of 'Differentiable' cannot be automatically synthesized in an extension in a different file to the type
extension Complex: Differentiable
^
swift-numerics/Sources/ComplexModule/Complex.swift:48:15: note: type declared here
public struct Complex<RealType> where RealType: Real {
              ^

Necessary after swiftlang/swift#31823, which removed the temporary default implementation of Differentiable.zeroTangentVectorInitializer.

Comment on lines +19 to +22
public var zeroTangentVectorInitializer: () -> Self {
{ Complex.zero }
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose not to use a memberwise implementation of zeroTangentVectorInitializer:

  public var zeroTangentVectorInitializer: () -> Self {
    { [realTanInit = real.zeroTangentVectorInitializer,
       imaginaryTanInit = imaginary.zeroTangentVectorInitializer] in
      Complex(realTanInit(), imaginaryTanInit())
    }
  }

I don't believe it's necessary for Complex.

@dan-zheng
Copy link
Contributor Author

cc @rxwei

@rxwei
Copy link
Member

rxwei commented Jun 9, 2020

Why was this broken? I thought we specifically made the patch that added zeroTangentVectorInitializer synthesis be source compatible.

@dan-zheng
Copy link
Contributor Author

zeroTangentVectorInitializer synthesis is source-compatible, except that cross-file synthesis is disallowed for all existing derived conformances. I think the diagnostic makes that clear.

We could consider lifting the same-file synthesis restriction just for Differentiable.zeroTangentVectorInitializer. I'm not sure if that has negative implications.

rxwei
rxwei previously approved these changes Jun 9, 2020
The `Differentiable.zeroTangentVectorInitializer` protocol requirement is a
closure producing a zero tangent vector, capturing minimal information from
`self`.

Fixes the `Complex: Differentiable` conformance for master toolchains.
@dan-zheng
Copy link
Contributor Author

@stephentyrone: could you please take a look?
This fix helps unblock apple/swift-numerics users using Swift master toolchains.

@stephentyrone stephentyrone merged commit c634b5d into apple:master Jun 9, 2020
@dan-zheng dan-zheng deleted the complex-differentiable branch June 9, 2020 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants