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

I agree with what Karl wrote above, and I think this is a misuse of the term "Darwin".

There are more than 4 Darwin systems. Although macOS, iOS, tvOS and watchOS are some of the most targeted ones, they're not the only ones. Within Apple, there are at least bridgeOS, audioOS, and whatever iPods used to run on; outside apple, there are at least OpenDarwin and PureDarwin. There can also be arbitrarily more Darwin systems in the future. Thus, as far as the current implementation goes, it is incorrect to equate Darwin to only macOS, iOS, tvOS, and watchOS.

Also, as far as I understand, if os(macOS) || os(iOS) || ... are used when there are APIs from the vendor (Apple) that are not available elsewhere. These APIs normally sit above the Darwin layer, e.g. SwiftUI and AppKit which is in the Cocoa layer. However, as mentioned in the paragraph above, there are more than the 4 Darwin systems, and not all of them have these APIs. So it's just wrong to replace something like

#if os(macOS) || os(iOS) || os(watchOS) || os(tvOS)
import CoreData

with something like

#if os(Darwin)
import CoreData // error if a Darwin system does not have Core Data

Since the only thing that all Darwin systems share is the Darwin layer itself, whose APIs are exposed via the Darwin module, the only correct code within an #if os(Darwin) would be code that uses only the Darwin module. And for this usage, we already have #if canImport(Darwin) which in my opinion expresses very clearly what the condition is.

9 Likes