A lot of code written to support more than one platform contains
#if arch() conditions to handle differences between 32-bit or 64-bit platforms or little and big endian CPUs. This may turn into long statements like
#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le), enumerating every (currently) supported architecture which must be handled by that
#if … #endif clause. 1
Checking each platform currently supported by Swift in order to conditionally compile 32-bit or 64-bit code does not express intent — it is just a list of platforms with certain implicit properties.
Furthermore, it is very fragile. Whenever a new platform is supported by Swift, it must be added to every source file that uses this technique to determine which code path to compile.
The current compilation conditions for
arch, etc should be amended by a
wordLength condition — or several in case different kinds of machine words need to be differentiated.
An equally valid spelling might be
This would replace a line such as
#if arch(x86_64) || arch(arm64) || arch(s390x) || arch(powerpc64) || arch(powerpc64le) with
#if wordLength(64) or even
Impact on existing code
This would not deprecate the current
arch() condition, so any existing code would still compile as usual.
if Int.bitWidth == Int64.bitWidth would ideally do the same thing as
#if intWidth(64). This is not guaranteed to be resolved at compile-time, though, so people still rely on checking the architecture with compilation conditionals. Compilation conditionals also have the advantage of being explicit about compile-time evaluation.