swift-system icon indicating copy to clipboard operation
swift-system copied to clipboard

Foundation additions

Open benrimmington opened this issue 5 years ago • 6 comments

Should the open-source Swift System have a new module, with the same Foundation additions as in the closed-source Swift System?

// MARK: - Foundation Additions

import Foundation

// Available when Foundation is imported with System
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
extension FilePath {

    /// Creates a file path from a URL
    ///
    /// The result is nil if `url` doesn't refer to a local file.
    public init?(_ url: URL)
}

// Available when Foundation is imported with System
@available(macOS 11.0, iOS 14.0, watchOS 7.0, tvOS 14.0, *)
extension URL {

    /// Creates a URL from a file path by by copying and validating its UTF-8 contents.
    ///
    /// This initializer does not try to repair ill-formed UTF-8 code unit
    /// sequences. If any are found, the result of the initializer is `nil`.
    ///
    public init?(_ path: FilePath)

    /// Creates a URL from a file path by by copying and validating its UTF-8 contents.
    ///
    /// This initializer does not try to repair ill-formed UTF-8 code unit
    /// sequences. If any are found, the result of the initializer is `nil`.
    ///
    public init?(_ path: FilePath, isDirectory: Bool)
}

benrimmington avatar Oct 17 '20 02:10 benrimmington

@lorentey do you know what the story is with cross-import overlays, especially in a package build environment?

milseman avatar Oct 21 '20 15:10 milseman

We can definitely have the code in System, but I'm not sure how the build system side of things would work out with SwiftPM. @abertelrud do you know how SPM handles cross-import modules?

milseman avatar Oct 23 '20 20:10 milseman

One thing we could do is to put the combo overlay APIs into a separate build product that must be imported explicitly.

lorentey avatar Oct 25 '20 01:10 lorentey

I think it would be better if these went the other way - i.e. in Foundation.

The reason is that the natural layering of these 2 libraries would have swift-system sit at the bottom, and for Foundation to depend on it and use it, and for swift-system not to depend on Foundation. For example, it would be fantastic if FS APIs such as FileManager would also accept a FilePath without requiring that it goes through String.

karwa avatar Jul 28 '21 11:07 karwa

@karwa that's certainly how things are starting to shake out, at least for implementation details. I think it would be nice to formally establish this layering. Foundation will (likely) forever pull in the Darwin module though, so we'll still have the C APIs as well as System.

@Catfish-Man thoughts?

milseman avatar Aug 05 '21 16:08 milseman

I think it would be better if these went the other way - i.e. in Foundation.

The reason is that the natural layering of these 2 libraries would have swift-system sit at the bottom, and for Foundation to depend on it and use it, and for swift-system not to depend on Foundation.

+1 on that, it's nice to be able to build server-side swift without pulling in Foundation at all (and swift-system is definitely useful for that context).

hassila avatar Dec 05 '21 11:12 hassila