Skip to content

Latest commit

 

History

History
18 lines (13 loc) · 801 Bytes

script.md

File metadata and controls

18 lines (13 loc) · 801 Bytes

autoclosure

Did you know how the autoclosure works in Swift programming? Well... The autoclosure attribute in Swift is like a bit of programming magic. It automatically turns an expression into a closure, making your code cleaner and more concise. Instead of manually wrapping expressions in closures, autoclosure does it for you, allowing you to write more natural and intuitive code. It is a handy tool, but like any potent spell, it should be used wisely to avoid making your code confusing.

func logIfTrue(_ predicate: @autoclosure () -> Bool) {
    if predicate() {
        print("It's true!")
    }
}

logIfTrue({ return 1 > 0 }) /// Without @autoclosure
logIfTrue(1 > 0) /// With @autoclosure

Reference

YouTube 👀