I noticed when writing an assert
in Swift that the first value is typed as
@autoclosure() -> Bool
with an overloaded method to return a generic T
value, to test existence via the LogicValue
protocol
.
However sticking strictly to the question at hand. It appears to want an @autoclosure
that returns a Bool
.
Writing an actual closure that takes no parameters and returns a Bool does not work, it wants me to call the closure to make it compile, like so:
assert({() -> Bool in return false}(), "No user has been set", file: __FILE__, line: __LINE__)
However simply passing a Bool works:
assert(false, "No user has been set", file: __FILE__, line: __LINE__)
So what is going on? What is @autoclosure
?
Edit: @auto_closure
was renamed @autoclosure
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…