Skip to content

Conform 'AnyDifferentiable' and 'AnyDerivative' to 'CustomReflectable'. #36487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions stdlib/public/Differentiation/AnyDifferentiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ public struct AnyDifferentiable: Differentiable {
}
}

extension AnyDifferentiable: CustomReflectable {
public var customMirror: Mirror {
Mirror(reflecting: base)
}
}

//===----------------------------------------------------------------------===//
// `AnyDerivative`
//===----------------------------------------------------------------------===//
Expand Down Expand Up @@ -365,6 +371,12 @@ public struct AnyDerivative: Differentiable & AdditiveArithmetic {
}
}

extension AnyDerivative: CustomReflectable {
public var customMirror: Mirror {
Mirror(reflecting: base)
}
}

//===----------------------------------------------------------------------===//
// Helpers
//===----------------------------------------------------------------------===//
Expand Down
20 changes: 20 additions & 0 deletions test/AutoDiff/stdlib/anydifferentiable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,16 @@ TypeErasureTests.test("AnyDifferentiable casting") {
expectEqual(nil, genericAny.base as? Generic<Double>)
}

TypeErasureTests.test("AnyDifferentiable reflection") {
let originalVector = Vector(x: 1, y: 1)
let vector = AnyDifferentiable(originalVector)
let mirror = Mirror(reflecting: vector)
let children = Array(mirror.children)
expectEqual(2, children.count)
expectEqual(["x", "y"], children.map(\.label))
expectEqual([originalVector.x, originalVector.y], children.map { $0.value as! Float })
}

TypeErasureTests.test("AnyDerivative casting") {
let tan = AnyDerivative(Vector.TangentVector(x: 1, y: 1))
expectEqual(Vector.TangentVector(x: 1, y: 1), tan.base as? Vector.TangentVector)
Expand All @@ -107,6 +117,16 @@ TypeErasureTests.test("AnyDerivative casting") {
expectEqual(nil, zero.base as? Generic<Float>.TangentVector)
}

TypeErasureTests.test("AnyDerivative reflection") {
let originalTan = Vector.TangentVector(x: 1, y: 1)
let tan = AnyDerivative(originalTan)
let mirror = Mirror(reflecting: tan)
let children = Array(mirror.children)
expectEqual(2, children.count)
expectEqual(["x", "y"], children.map(\.label))
expectEqual([originalTan.x, originalTan.y], children.map { $0.value as! Float })
}

TypeErasureTests.test("AnyDifferentiable differentiation") {
// Test `AnyDifferentiable` initializer.
do {
Expand Down