[swift-evolution] Constant initialization and unreachable code.

Alex Martini amartini at apple.com
Wed Oct 19 11:41:42 CDT 2016

> On Oct 19, 2016, at 9:37 AM, Alex Martini via swift-evolution <swift-evolution at swift.org> wrote:
>> On Oct 19, 2016, at 4:50 AM, David Goodine via swift-evolution <swift-evolution at swift.org <mailto:swift-evolution at swift.org>> wrote:
>> Hey all,
>> I don’t know if this is really an ‘evolution’ topic per se, and I’m not on Swift Dev, but thought someone here could shed some light on this.
>> Often when developing code, if I need create mode switches (constant Bools) so that I can move back and forth between different features/implementations for testing, developing experimental features, migration, etc.  Currently if I write the following:
>> let useFoo = true
>> if useFoo {
>> // Foo code
>> } else {
>> // Non-Foo code
>> }
> In your debugging, do you actually need this condition to be evaluated at runtime?  For example, in the debugger, are you changing the value of useFoo at runtime to switch which branch is used?
> If not, maybe #if would be better — it makes the decision at compile time.  I don't get any warnings from the following code:
> let useFoo = true
> #if useFoo
>     print("foo code")
> #else
>     print("non-foo code")
> #endif

Sorry, I hit Send too soon.  That listing should read:

#if useFoo
    print("foo code")
    print("non-foo code")

You switch useFoo on and off by passing "-D useFoo" to the compiler.  For example:

% swift test.swift
non-foo code

% swift -D useFoo test.swift
foo code

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20161019/cdab6f8e/attachment.html>

More information about the swift-evolution mailing list