[swift-users] Wrapping function declarations in #if swift()

Erica Sadun erica at ericasadun.com
Fri May 13 10:11:24 CDT 2016


> On May 12, 2016, at 11:38 PM, Tyler Fleming Cloutier via swift-users <swift-users at 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.

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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160513/22ddb386/attachment.html>


More information about the swift-users mailing list