Adding missing Units and Conversions to Foundation

Hi,

I was wondering what the Swift team’s stance/policy was on adding established missing Units and Conversions to the Measurement APIs (https://developer.apple.com/documentation/foundation/unit).

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?

1 Like

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.

8 Likes

Thanks for following up. Will do as soon as I get some time - hopefully, by the weekend.

1 Like

I didn’t have time this weekend, but will do my best to get to it by mid-February.

Wow, Foot-candle is a real thing. The imperial system never fails to disappoint me.

I would like to see a UnitDataStorage (byte) and UnitForce (Newton)

4 Likes

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: https://github.com/ole/Ampere). 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.

If you’re looking for units to add, check out the CGS system, which includes a bunch of units that aren’t implemented. In particular:

Quantity Unit Comparison
Speed cm/s 10-2 m/s
Acceleration gal 10-2 m/s2
Energy erg 10-7 J
Power erg/s 10-7 W
Pressure barye 10-1 Pa

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.

2 Likes

This video provides a great starting point for the minimal set of units that should be provided by UnitLength: https://www.youtube.com/watch?v=r7x-RGfd0Yk 😜

6 Likes

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 ...
}
3 Likes

@Tony_Parker When you say ICU support, are you referring to this? http://site.icu-project.org/#TOC-What-is-ICU-

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.