Last active
December 13, 2016 05:26
-
-
Save kasrak/2b79c458713cf77db632 to your computer and use it in GitHub Desktop.
Swift: combining varargs and autoclosure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This compiles: | |
func immediateOr(args:Bool...) -> Bool { | |
for arg in args { | |
if arg { | |
return true | |
} | |
} | |
return false | |
} | |
// A. This doesn't compile: | |
func lazyOr(args: @autoclosure() -> Bool...) -> Bool { | |
for arg in args { // "Error: Function produces expected type '[@autoclosure() -> Bool]'" | |
if arg() { | |
return true | |
} | |
} | |
return false | |
} | |
// B. This doesn't compile (typealias): | |
typealias LazyPredicate = @autoclosure() -> Bool | |
func lazyOr(args: LazyPredicate...) -> Bool { | |
for arg in args { // "Error: Function produces expected type '[@autoclosure() -> Bool]'" | |
if arg() { | |
return true | |
} | |
} | |
return false | |
} | |
// C. This doesn't compile (parenthesize): | |
func lazyOr(args: (@autoclosure() -> Bool)...) -> Bool { | |
for arg in args { // "Error: Function produces expected type '[@autoclosure() -> Bool]'" | |
if arg() { | |
return true | |
} | |
} | |
return false | |
} | |
// D. This "works"... | |
func lazyOr(args: (@autoclosure() -> Bool, Int)...) -> Bool { | |
for arg in args { | |
if arg.0() { | |
return true | |
} | |
} | |
return false | |
} | |
lazyOr((expensiveA(), 42), (expensiveB(), 42)) |
I was excited when I saw the comment by @vivekseth, but I've now tried it and it looks like it doesn't work with Swift 3.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Have you considered using an array parameter instead of a variadic parameter?
According to Apple's Documentation variadic parameters are made available to a function as a constant array anyway.
You might have some luck pinpointing exactly why you're getting compiler errors by taking a look at Swift's Grammar Specification.
Hope this helps!
Prints: