Swift: Preprocessor vs Conditional Compilation
Chances that you want to add some lines of code during compilation using preprocessor and you’ll miss it in Swift.
Swift doesn’t support Preprocessor like in C.
Preprocessor
This pseudo code will work in preprocessor
#if RELEASE
fileprivate
#else
class Foo {}
The code above declare Foo as fileprivate during RELEASE, there are tons of scenarios to do this messy thing.
Conditional Compilation
In Swift, to achive to above goal, we need to do conditional compilation.
#if RELEASE
fileprivate class Foo {}
#else
class Foo {}
#endif
That’s the current possible solution.
Partial Conditional Compilation
Ah, so you think we can do this?
#if RELEASE
fileprivate class Foo {
#else
class Foo {
#endif
}
You can’t. As of my writing with Swift 5.0. But let’s see the compiler source code. Why is this isn’t possible. The first obvious reason is that above code is preprocessor mental model not conditional compilation.
I need to learn a lot.