(This is a follow-up on Get Swift language version programmatically which I started in “Using Swift”.)
I suggest to add some mechanism to get Swift language version information programmatically. For example:
print(#swiftVersionInformation)
producing
Apple Swift version 4.2-dev
// or
Apple Swift version 4.2-dev (LLVM 49e6f88643, Clang cf4f73cfc1, Swift 53be298f1f)
similar to what the swift REPL prints as welcome prompt (apparently in void Driver::printVersion
).
Motivation:
I often switch between different Xcode apps (such as the current release and the latest beta). In addition I sometimes install additional toolchains, e.g. the “latest snapshot”, and it is not immediately obvious (to me) which Swift version such a toolchain corresponds to.
Therefore – only for information purposes! – some version string would be convenient to see what I am currently working with in my test programs.
The alternative would be a sequence of conditional compilation statements
#if swift(>=4.2)
print("Swift 4.2")
#elseif swift(>=4.1)
print("Swift 4.1")
#elseif swift(>=4.0)
...
#endif
which need the explicit knowledge of all available versions and must be maintained for new versions.
Please note:
This is not meant as a mechanism to check for Swift features or for conditional compilation depending on the compiler version, but solely for information purposes, when playing around with different Xcode apps or toolchains.
That also means that the exact format of that version string would not matter, it is not meant to be parsed to evaluated in any way.