I was wondering what the Swift team's stance/policy was on adding established missing Units and Conversions to the Measurement APIs (Apple Developer Documentation).
Here's one small example of an extension I carry around in a project:
// MARK: - Converts Lux to Footcandles
extension UnitIlluminance {
static let footCandle = UnitIlluminance(symbol: "Fc", converter: UnitConverterLinear(coefficient: 10.7639150512))
}
Would submissions to the core-libs-foundation of these types be accepted or rejected?
Additions to Foundation are not out of the question; they just require a bit more effort since we would want pairity between the objective-c implementation and the swift implementation. Internally to Cocoa we follow a very similar process to Swift evolution so a proposal written for swift-evo would be suitable to pass across to Cocoa as well. The only hitch to getting it done is that the maintainers of NSMeasurement and NSUnit would need to sign off on supporting the change and the proposal would have to be approved. In the end it just means a second level of process. If you are interested in giving that a go, write up a proposal using the swift evolution template and get me a link to it.
Yup, it surprises me that force is missing, as are a lot of other physical constants (however, @ole has a nice library adding many of the simpler ones here: GitHub - ole/Ampere: Adding multiplication and division to the units of measurement types in Foundation.). One unit that's missing that irls me is electronvolts under energy. However, I assume that electronvolts are missing because it's not an SI unit, even though its heavily used in Atomic and Nuclear Physics.
If anyone has any other proposals, please drop here over the next week or two, and I'll add them into the list.
Yup, on my list! My hope is for the Measurement suite of APIs to be easily usable in the natural sciences, and the best way to get there is to implement all of the SI-Units, MKS units, CGS-units, and some obscure, less often used, but equally important units.
By the way, one of the considerations here of adding it to Foundation in the past has been ICU support. The measurement itself is one thing, but we want it to be usable with MeasurementFormatter as well if possible.
On a more serious note, one unit I have defined for myself in a few projects is UnitLength.typographicPoints. I find this useful for converting lengths in millimeters to points, e.g. for rendering into a PDF.
Here's my definition:
extension UnitLength {
/// 1 pt = 1/72 inch ≅ 0.352777778 mm = 0.000352777778 m
static let typographicPoints = UnitLength(symbol: "pt",
converter: UnitConverterLinear(coefficient: 0.352777778e-3))
}
And here's a usage example: creating a PDF renderer for an A4 paper size:
import UIKit
// A4 paper: 210 × 297 mm
let a4Width = Measurement(value: 210, unit: UnitLength.millimeters)
let a4Height = Measurement(value: 297, unit: UnitLength.millimeters)
// PDF requires points
let a4PageRect = CGRect(
x: 0, y: 0,
width: a4Width.converted(to: .typographicPoints).value,
height: a4Height.converted(to: .typographicPoints).value
)
let pdfRenderer = UIGraphicsPDFRenderer(bounds: a4PageRect)
let pdfData = pdfRenderer.pdfData { context in
// Draw ...
}
Right - we use ICU (and CLDR) data to format the measurements correctly. For example, CLDR is what tells us to use kilometers in Canada and miles in the US.