#if __CORELIBS_FOUNDATION__


(Drew Crawford) #1

Hi folks,

I notice that swift-corelibs-foundation is (apparently) deliberately incompatible with Darwin Foundation (https://github.com/apple/swift-corelibs-foundation/blame/master/Foundation/NSFileManager.swift#L324) at least for the immediate future.

I'm wondering if we can get an #if that would let me write conditional code.

Obviously the "real" answer is to fix the bugs, but (at least on my side) that will happen a lot faster if I "can" easily build the same codebase against corelibs-foundation and darwin-foundation.


(Tony Parker) #2

Hi Drew,

This difference is a consequence of the lack of bridging between types like String and NSString.

If this API returned [String : AnyObject], then the dictionary could not contain String (because String is not an object).

This is something we will have to fix up in some comprehensive way in both Darwin and CoreLibs Foundation, but there is no answer yet.

In this particular case, how would you use the #if? Any should be source compatible with AnyObject, since Any is a superset of AnyObject, right?

- Tony

···

On Jan 1, 2016, at 11:26 PM, Drew Crawford via swift-corelibs-dev <swift-corelibs-dev@swift.org> wrote:

Hi folks,

I notice that swift-corelibs-foundation is (apparently) deliberately incompatible with Darwin Foundation (https://github.com/apple/swift-corelibs-foundation/blame/master/Foundation/NSFileManager.swift#L324) at least for the immediate future.

I'm wondering if we can get an #if that would let me write conditional code.

Obviously the "real" answer is to fix the bugs, but (at least on my side) that will happen a lot faster if I "can" easily build the same codebase against corelibs-foundation and darwin-foundation.

_______________________________________________
swift-corelibs-dev mailing list
swift-corelibs-dev@swift.org
https://lists.swift.org/mailman/listinfo/swift-corelibs-dev


(Drew Crawford) #3

Well I don't know what you mean by "compatible" but the thing about strongly typed languages is we can't do

let a: Any

foo(a: AnyObject) { /* */ }

foo(a)

because that is "type error". We can of course cast, but if in Darwin a.self is already AnyObject, casting again to AnyObject produces a warning, and I try to not have any warnings in my code.

This is kind of a side quest–there are a lot of ways to work around these problems–but the obviously straightforward one was to use #if to conditionally cast, and that wasn't available.

More broadly I find myself using the #if that I've created very often–my workflow seems to be to create an extension on an NSClass in-file that adds missing functionality and then PR it to corelibs-foundation afterwards–and conditional compilation really helps with that workflow.

···

On Jan 4, 2016, at 3:09 PM, Tony Parker <anthony.parker@apple.com> wrote:

In this particular case, how would you use the #if? Any should be source compatible with AnyObject, since Any is a superset of AnyObject, right?


(Tony Parker) #4

Hi Drew,

···

On Jan 5, 2016, at 12:08 AM, Drew Crawford <drew@sealedabstract.com> wrote:

On Jan 4, 2016, at 3:09 PM, Tony Parker <anthony.parker@apple.com <mailto:anthony.parker@apple.com>> wrote:

In this particular case, how would you use the #if? Any should be source compatible with AnyObject, since Any is a superset of AnyObject, right?

Well I don't know what you mean by "compatible" but the thing about strongly typed languages is we can't do

let a: Any

foo(a: AnyObject) { /* */ }

foo(a)

because that is "type error". We can of course cast, but if in Darwin a.self is already AnyObject, casting again to AnyObject produces a warning, and I try to not have any warnings in my code.

This is kind of a side quest–there are a lot of ways to work around these problems–but the obviously straightforward one was to use #if to conditionally cast, and that wasn't available.

More broadly I find myself using the #if that I've created very often–my workflow seems to be to create an extension on an NSClass in-file that adds missing functionality and then PR it to corelibs-foundation afterwards–and conditional compilation really helps with that workflow.

My main objection is that I don’t want us to require clients to #if at all, in the long term. Putting it in now means that it will be easier to say “just #if” instead of fixing the underlying source incompatibility problem.

I think that if we get to a time nearer to the finish line of Swift 3 and we still have many source incompatible changes between the Core Libs Foundation and a corresponding Darwin Foundation then we should introduce something like the #if.

- Tony