Skip to content

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

Closed

Conversation

Dante-Broggi
Copy link

This is implementing the NSUnimplemented methods.

@ianpartridge
Copy link
Contributor

Thanks for your contribution! Can you add some tests too?

P.S. We use 4 spaces for indentation here.

guard aCoder.allowsKeyedCoding else {
preconditionFailure("Unkeyed coding is unsupported.")
}
aCoder.encode(self.doubleValue, forKey:"NS.dblval")
Copy link
Contributor

Choose a reason for hiding this comment

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

NS.value

@Dante-Broggi
Copy link
Author

For tests, as there isn't a TestNSMeasurement.swift file, do I have to do anything special when adding it?

@millenomi
Copy link
Contributor

Please add it to the Xcode project and the list of test source files in the build.py file when you do.

@Dante-Broggi
Copy link
Author

The build.py includes + glob.glob('./TestFoundation/Test*.swift')) # all TestSomething.swift are considered sources to the test project in the TestFoundation directory so, actually I don't need to add TestNSMeasurement.swift, but I did add it to the Xcode project.

@spevans
Copy link
Contributor

spevans commented Feb 9, 2018

@swift-ci please test

@spevans
Copy link
Contributor

spevans commented Feb 9, 2018

I really should not have needed CI to find these, oh well.
@spevans
Copy link
Contributor

spevans commented Feb 9, 2018

@swift-ci please test

@spevans
Copy link
Contributor

spevans commented Feb 9, 2018

@Dante-Broggi
Copy link
Author

So, the one thing I couldn't fix is canBeConverted(to:), the original implementation of which used objc-only isKind(of:) and I cannot think of how to replace that.
The nearest I can think of is (otherUnit is type(of: unit)), except is would require a dynamic metatype argument on the rhs, and it currently only accepts static metatypes.

`canBeConverted(to:)` uses `isKind(of:)` which is only available with objc.
@Dante-Broggi
Copy link
Author

I thought of something that doesn't use .isKind(of:) for canBeConverted(to:)
just attempt the conversion, and return false instead of fatalError()ing.

@spevans
Copy link
Contributor

spevans commented Feb 11, 2018

@swift-ci please test

return Measurement(value: self.doubleValue + rhs.value, unit: self.unit)
} else {
guard let dimension = unit as? Dimension,
let otherDimension = otherUnit as? Dimension else {
Copy link
Contributor

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)

Copy link
Author

Choose a reason for hiding this comment

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

Yes, indeed.

} else {
return unit.isEqual(otherUnit)
}
re
Copy link
Contributor

Choose a reason for hiding this comment

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

Extra typo here

@spevans
Copy link
Contributor

spevans commented Feb 11, 2018

@swift-ci please test

@spevans
Copy link
Contributor

spevans commented Feb 11, 2018

@swift-ci please test

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))")
}
Copy link
Contributor

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?

Copy link
Author

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. 🙁

// just check conversion
if let dimension = unit as? Dimension,
let otherDimension = otherUnit as? Dimension {
return true
Copy link
Contributor

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.

Copy link
Author

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.

@spevans
Copy link
Contributor

spevans commented Feb 11, 2018

@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 swift-corelibs-foundation repo you also need to checkout the swift-corelibs-xctest repo.

Now in the swift-corelibs-foundation directory, open the Foundation.xcworkspace workspace (NOT the xcodeproj). The workspace includes the Foundation Xcode project and the XCTest Xcode project.

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.

@spevans
Copy link
Contributor

spevans commented Feb 11, 2018

@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)
Copy link
Contributor

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
Copy link
Contributor

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))")
Copy link
Contributor

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 {
Copy link
Contributor

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")
Copy link
Contributor

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()`?
Copy link
Contributor

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?

Copy link

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!"

@alblue
Copy link
Contributor

alblue commented Aug 7, 2018

@swift-ci please test

@millenomi
Copy link
Contributor

@Dante-Broggi are you still interested in chaperoning this patch?

@ikesyo
Copy link
Member

ikesyo commented May 25, 2019

Looks like this is superseded by #2095.

@ikesyo ikesyo closed this May 25, 2019
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.

9 participants