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.