Wrapping function declarations in #if swift()


(Tyler Cloutier) #1

Hello everyone,

It does seem like it is currently possible to wrap just the function declaration in an #if swift() directive like so:

#if swift(>=3.0)
public func add(filter filterName: String, path: String) {
#else // ERROR Expected ‘}’ at end of brace statement
public func addFilter(filterName: String, path: String) {
#endif

Is it possible I’m missing how to do this? This is particularly painful in Swift 3 given the change to move have labels on the first function parameter by default. As far as I can see it means that I am required to wrap the entire function body even if nothing else is incompatible with Swift 3.

Is this just an implementation detail that will be changed?

Thanks,

Tyler


(Marco S Hyman) #2

Hello everyone,

It does seem like it is currently possible to wrap just the function declaration in an #if swift() directive like so:

#if swift(>=3.0)
public func add(filter filterName: String, path: String) {
#else // ERROR Expected ‘}’ at end of brace statement
public func addFilter(filterName: String, path: String) {
#endif

Swift conditional compilation is not line based. SE-0020 said:

"Like other build configurations, #if swift isn't line-based - it encloses whole statements or declarations. However, unlike the others, the compiler won't parse inactive branches guarded by #if swift or emit lex diagnostics, so syntactic differences for other Swift versions can be in the same file.”

https://github.com/apple/swift-evolution/blob/master/proposals/0020-if-swift-version.md

Is it possible I’m missing how to do this? This is particularly painful in Swift 3 given the change to move have labels on the first function parameter by default. As far as I can see it means that I am required to wrap the entire function body even if nothing else is incompatible with Swift 3.

My solution: a git branch for 2.x and a git branch for 3.x. Eventually the 2.x branch will wither away.

Marc

···

On May 12, 2016, at 10:38 PM, Tyler Fleming Cloutier via swift-users <swift-users@swift.org> wrote:


(Erica Sadun) #3

Alternatively, you could move the function body out to a separate closure and call it from differentiated 3 and 2.2 signatures.

I may have written a blog post about this this morning.

-- Erica

···

On May 12, 2016, at 11:38 PM, Tyler Fleming Cloutier via swift-users <swift-users@swift.org> wrote:

Hello everyone,

It does seem like it is currently possible to wrap just the function declaration in an #if swift() directive like so:

#if swift(>=3.0)
public func add(filter filterName: String, path: String) {
#else // ERROR Expected ‘}’ at end of brace statement
public func addFilter(filterName: String, path: String) {
#endif

Is it possible I’m missing how to do this? This is particularly painful in Swift 3 given the change to move have labels on the first function parameter by default. As far as I can see it means that I am required to wrap the entire function body even if nothing else is incompatible with Swift 3.


(Tyler Cloutier) #4

I see. Thanks for the quick reply Marc!

···

On May 12, 2016, at 11:05 PM, Marco S Hyman <marc@snafu.org> wrote:

On May 12, 2016, at 10:38 PM, Tyler Fleming Cloutier via swift-users <swift-users@swift.org> wrote:

Hello everyone,

It does seem like it is currently possible to wrap just the function declaration in an #if swift() directive like so:

#if swift(>=3.0)
public func add(filter filterName: String, path: String) {
#else // ERROR Expected ‘}’ at end of brace statement
public func addFilter(filterName: String, path: String) {
#endif

Swift conditional compilation is not line based. SE-0020 said:

"Like other build configurations, #if swift isn't line-based - it encloses whole statements or declarations. However, unlike the others, the compiler won't parse inactive branches guarded by #if swift or emit lex diagnostics, so syntactic differences for other Swift versions can be in the same file.”

https://github.com/apple/swift-evolution/blob/master/proposals/0020-if-swift-version.md

Is it possible I’m missing how to do this? This is particularly painful in Swift 3 given the change to move have labels on the first function parameter by default. As far as I can see it means that I am required to wrap the entire function body even if nothing else is incompatible with Swift 3.

My solution: a git branch for 2.x and a git branch for 3.x. Eventually the 2.x branch will wither away.

Marc