WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Day 7 returning the closure from the function

Forums > 100 Days of Swift

Why the below code is legal swift statement?

func createValidator() -> (String) -> Bool {
    return {
        if $0 == "twostraws" {
            return true
        } else {
            return false
        }
    }
}
let validator = createValidator()
print(validator("twostraws"))

I thought return type is not matched up since closure returns Bool but function returns String.

1      

Excellent question!

This is a skill you need to develop and practice!

You need to learn how to read the signature of a function like a sentence. It's a skill, and you need to practice.

func createValidator() -> (String) -> Bool 
     {....snip.....}

Say it OUT LOUD until it makes sense.

createValidator() is a function that returns another function. The function it returns is a function that takes a String as a parameter, and returns a Bool.

Now take a look at the guts of your function.

// what are you returning here?
return {
        // This is a function expressed in closure syntax.
        // $0 means this function requires a parameter. 
        // Because it's compared to "twostraws" 
        // the compiler infers $0 must be a string. This function REQUIRES a string.
        if $0 == "twostraws" {
            return true  // this function returns a Bool
        } else {
            return false // again, returns a Bool. Change this to 42. How does the compiler respond?
        }
    }

Things to note: createValidator() does not take any parameters. createValidator() returns a function

If you hold your mouse over the variable validator and hold down the option key, XCode turns your cursor into a "?". CLICK the variable, and XCode will tell you what type validator is.

Take note! validator is NOT a createValidator() function. Instead, it's the function RETURNED by a createValidator() function.

2      

Here's another example. Paste this code into Playgrounds.

Convince yourself that each function has the same signature! Each possible function takes a String and returns a Bool.

// For: young92
// By: Obelix, 23 Jan 2022
// Paste into playgrounds.

func createValidator() -> (String) -> Bool {
    let pickOne = Int.random(in: 1...3)
    // pick a random function to return
    // each one has the same signature!
    if pickOne == 1 {
        return {
            // take a string, return a bool
            print ("Random function #1 \($0)")
            return false
        }
    } else if pickOne == 2 {
        return {
            // take a string, return a bool
            print ("Random function #2 \($0)")
            return true
        }
    } else {
        return {
            // take a string, return a bool
            print ("Random function #3 \($0)")
            return Bool.random() // maybe true? could be false
        }
    }
}
let randomFunction = createValidator()  // what TYPE is randomFunction?
print(randomFunction("feeling lucky?"))

3      

Thanks for the excellent explanation! It makes much sense now.

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Optimize your app’s startup time, binary size, and overall performance using Emerge’s advanced app optimization and monitoring tools. Reliably measure app size, speed up your app's startup time with Emerge's Launch Booster, and much more. Emerge is actively used by many of the top mobile development teams in the world.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.