`#if os(Darwin)`, a shorthand for checking for Darwin platforms

I actually think this is correct. If you're trying to guard import Darwin and uses of the Darwin module, then isn't canImport(Darwin) expressing precisely the thing you want?

What exactly is the API surface of the os(Darwin) SDK? Can I assume only that it includes the Darwin module? If so, is it not a synonym for canImport(Darwin)?

Or does os(Darwin) also imply the availability of other Apple frameworks, such as SwiftUI? What about frameworks that are not available on every Apple platform, such as AppKit and UIKit, or the recently-announced ClockKit?

Correct me if I'm wrong, but I don't believe Apple has ever defined a set of frameworks that all Darwin-family platforms will support. Pretty much the only thing that seems reasonable to assume is that import Darwin will work on a Darwin platform (kind of, by definition). IMO, it's up to Apple's platform team whether or not they want to introduce an expanded concept of a "Darwin OS" which includes more than just that one module, and which other modules os(Darwin) should include.

On a related note, hopefully one day we'll add some kind of CStdlib module, which will cut down the boilerplate for cross-platform code. So you could just import CStdlib, and handle the platform differences as and where they occur.

5 Likes