> On Oct 19, 2016, at 9:37 AM, Alex Martini via swift-evolution 
> <[email protected]> wrote:
> 
>> 
>> On Oct 19, 2016, at 4:50 AM, David Goodine via swift-evolution 
>> <[email protected] <mailto:[email protected]>> 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")
#else
    print("non-foo code")
#endif

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

_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to