-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Implement NSMeasurement #1425
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
Implement NSMeasurement #1425
Conversation
I am thuroughly unsure of the keys (`"NS.dblval"` and `"NS.unit"`) I used in these methods.
Thanks for your contribution! Can you add some tests too? P.S. We use 4 spaces for indentation here. |
Foundation/NSMeasurement.swift
Outdated
guard aCoder.allowsKeyedCoding else { | ||
preconditionFailure("Unkeyed coding is unsupported.") | ||
} | ||
aCoder.encode(self.doubleValue, forKey:"NS.dblval") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NS.value
For tests, as there isn't a |
Please add it to the Xcode project and the list of test source files in the build.py file when you do. |
Tests don't actually test anything yet.
The |
@swift-ci please test |
Looks like there were a couple of compilation errors, see https://ci.swift.org/view/Pull%20Request/job/swift-corelibs-foundation-PR-Linux/635/consoleFull#18229161593122a513-f36a-4c87-8ed7-cbc36a1ec144 |
I really should not have needed CI to find these, oh well.
@swift-ci please test |
@Dante-Broggi there are still a couple of compile errors in CI: https://ci.swift.org/view/Pull%20Request/job/swift-corelibs-foundation-PR-Linux/636/console |
So, the one thing I couldn't fix is |
`canBeConverted(to:)` uses `isKind(of:)` which is only available with objc.
I thought of something that doesn't use |
@swift-ci please test |
Foundation/NSMeasurement.swift
Outdated
return Measurement(value: self.doubleValue + rhs.value, unit: self.unit) | ||
} else { | ||
guard let dimension = unit as? Dimension, | ||
let otherDimension = otherUnit as? Dimension else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean rhs
instead of otherUnit
? (same on line 82)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, indeed.
Foundation/NSMeasurement.swift
Outdated
} else { | ||
return unit.isEqual(otherUnit) | ||
} | ||
re |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Extra typo here
@swift-ci please test |
@swift-ci please test |
Foundation/NSMeasurement.swift
Outdated
guard let dimension = unit as? Dimension, | ||
let otherDimension = rhs.unit as? Dimension else { | ||
fatalError("Cannot convert differing units that are non-dimensional! lhs: \(type(of: dimension)) rhs: \(type(of: otherDimension))") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be (type(of: unit))
and (type(of: rhs.unit))
as dimension
and otherDimension
don't exist in th guard
block if the as?
fails.
btw, are you testing on macOS or Linux?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am on a mac, but I really have no idea how to actually even build any of the swift projects.
So, realistically, nowhere. I am sorry, but I am just an enthusiast with no idea how to use the infrastructure. 🙁
Foundation/NSMeasurement.swift
Outdated
// just check conversion | ||
if let dimension = unit as? Dimension, | ||
let otherDimension = otherUnit as? Dimension { | ||
return true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
dimension
and otherDimension
are not used so could be replaced with _
or perhaps rewrite as unit is Dimension && otherUnit is Dimension
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I thought of that, then forgot to do it.
@Dante-Broggi Ok here's what to do assuming you are on either Sierra (10.12) or High Sierra (10.13) and have Xcode already installed: Get the latest snapshot from https://swift.org/download/#snapshots Trunk development (master) 'Xcode' . This is a package you install on your mac. Next, in Xcode, goto Xcode > Toolchains and you should see the snapshot you just downloaded in the menu, select it as your default tool chain. It will be called something like 'Swift Development Snapshot 2018-02-08 (a)' or something like that. Now in the same directory that you checked out the Now in the In the menu at the top the Xcode window, select 'TestFoundation > My Mac' then do 'Product > Run' and that will compile up CodeFoundation, Swift Foundation, XCtest and TestFoundation and then it will try and run the tests. Any compile errors should show up here. Don't worry about all of the warnings about deprecation in code that you haven't changed, there is usually because of changes in the main swift core in the snapshot you downloaded. That should be enough to get you started, let me know if you have any questions about the above. |
@swift-ci please test |
@@ -23,21 +23,93 @@ open class NSMeasurement : NSObject, NSCopying, NSSecureCoding { | |||
self.unit = unit | |||
} | |||
|
|||
open func canBeConverted(to unit: Unit) -> Bool { NSUnimplemented() } | |||
open func canBeConverted(to otherUnit: Unit) -> Bool { | |||
#if DEPLOYMENT_RUNTIME_OBJC //|| os(Linux) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the stuff inside a compilation conditional should be indented as though there weren't a conditional surrounding it; in most files, the conditional statement itself should be outdented to the left margin, but the specific style can vary depending on the file.
} else { | ||
return unit.isEqual(otherUnit) | ||
} | ||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this implementation match Darwin Foundation? It seems to be very limited.
} else { | ||
guard let dimension = unit as? Dimension, | ||
let otherDimension = otherUnit as? Dimension else { | ||
fatalError("Cannot convert differing units that are non-dimensional! lhs: \(type(of: unit)) rhs: \(type(of: otherUnit))") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Try to align these error messages as closely as possible with Darwin Foundation.
return Measurement(value: doubleValue, unit: otherUnit) | ||
} else { | ||
guard let dimension = unit as? Dimension, | ||
let otherDimension = otherUnit as? Dimension else { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: the stuff inside the braces should be indented only four spaces as compared to guard
; it's fine to indent the second line of the condition but it doesn't affect how the inside stuff is indented (the same applies elsewhere below).
guard aCoder.allowsKeyedCoding else { | ||
preconditionFailure("Unkeyed coding is unsupported.") | ||
} | ||
aCoder.encode(self.doubleValue, forKey:"NS.value") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: space after forKey:
; likewise one line below.
let doubleValue = aDecoder.decodeDouble(forKey: "NS.value") | ||
let possibleUnit = aDecoder.decodeObject(forKey: "NS.unit") | ||
guard let unit = possibleUnit as? Unit else { | ||
return nil // or should we `fatalError()`? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does Darwin Foundation do?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing it does is validation, e.g. @"Cannot add measurements of differing unit types!"
@swift-ci please test |
@Dante-Broggi are you still interested in chaperoning this patch? |
Looks like this is superseded by #2095. |
This is implementing the
NSUnimplemented
methods.