UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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.

3      

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.

5      

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?"))

6      

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

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.