Skip to content

[Foundation overlay] Enable custom AnyHashable representation for NSMeasurement #4793

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
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
5 changes: 0 additions & 5 deletions stdlib/public/SDK/Foundation/Measurement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,10 +227,6 @@ extension Measurement : _ObjectiveCBridgeable {
}
}

/*
FIXME(id-as-any): can't write this code because of:
<rdar://problem/27539951> "unhandled generic bridged type" when bridging NSMeasurement

@available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
extension NSMeasurement : _HasCustomAnyHashableRepresentation {
// Must be @nonobjc to avoid infinite recursion during bridging.
Expand All @@ -239,7 +235,6 @@ extension NSMeasurement : _HasCustomAnyHashableRepresentation {
return AnyHashable(self as Measurement)
}
}
*/

// This workaround is required for the time being, because Swift doesn't support covariance for Measurement (26607639)
@available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
Expand Down
30 changes: 30 additions & 0 deletions test/stdlib/TestMeasurement.swift
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,34 @@ class TestMeasurement : TestMeasurementSuper {
expectTrue(fiveKM < sevenThousandM)
expectTrue(fiveKM <= fiveThousandM)
}

func test_AnyHashableContainingMeasurement() {
let values: [Measurement<UnitLength>] = [
Measurement(value: 100, unit: UnitLength.meters),
Measurement(value: 100, unit: UnitLength.kilometers),
Measurement(value: 100, unit: UnitLength.kilometers),
]
let anyHashables = values.map(AnyHashable.init)
expectEqual(Measurement<UnitLength>.self, type(of: anyHashables[0].base))
expectEqual(Measurement<UnitLength>.self, type(of: anyHashables[1].base))
expectEqual(Measurement<UnitLength>.self, type(of: anyHashables[2].base))
expectNotEqual(anyHashables[0], anyHashables[1])
expectEqual(anyHashables[1], anyHashables[2])
}

func test_AnyHashableCreatedFromNSMeasurement() {
let values: [NSMeasurement] = [
NSMeasurement(doubleValue: 100, unit: UnitLength.meters),
NSMeasurement(doubleValue: 100, unit: UnitLength.kilometers),
NSMeasurement(doubleValue: 100, unit: UnitLength.kilometers),
]
let anyHashables = values.map(AnyHashable.init)
expectEqual(Measurement<Unit>.self, type(of: anyHashables[0].base))
expectEqual(Measurement<Unit>.self, type(of: anyHashables[1].base))
expectEqual(Measurement<Unit>.self, type(of: anyHashables[2].base))
expectNotEqual(anyHashables[0], anyHashables[1])
expectEqual(anyHashables[1], anyHashables[2])
}
}

#if !FOUNDATION_XCTEST
Expand All @@ -159,6 +187,8 @@ if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
MeasurementTests.test("testMeasurementFormatter") { TestMeasurement().testMeasurementFormatter() }
MeasurementTests.test("testEquality") { TestMeasurement().testEquality() }
MeasurementTests.test("testComparison") { TestMeasurement().testComparison() }
MeasurementTests.test("test_AnyHashableContainingMeasurement") { TestMeasurement().test_AnyHashableContainingMeasurement() }
MeasurementTests.test("test_AnyHashableCreatedFromNSMeasurement") { TestMeasurement().test_AnyHashableCreatedFromNSMeasurement() }
runAllTests()
}
#endif