Skip to content

Latest commit

 

History

History
84 lines (64 loc) · 1.8 KB

day6(Closures-1).md

File metadata and controls

84 lines (64 loc) · 1.8 KB

Day 6

Closures-1 📖

It is a special operation similar to the assignment but functionalizing.

Usage 🔨

Basic Closures

let example = {
  print("Hello, I'm an example.")
}

example() // Hello, I'm an example.

Accepting Parameters

// Since they are functions, of course they can take parameters.
let example = { (yourExample : String, waitWhat : String) in
  print(yourExample + waitWhat)
}

example("Haha that's my example"," We don't care about names. We use indexes.") // Haha that's my example We don't care about names. We use indexes.

Returning Closures

let iReturn = { (message : String) -> String in
  return "Your message : \(message) \nI call you back." // "\n" allows jumping to the next line

}

let result = iReturn("Where are you?")
print(result) 
// Your message : Where are you? 
// I call you back.

Closures as Parameters

let exampleClos = { 
    print("I'm actually parameter")
}

func funcExample(clos: () -> Void) {
    print("What?")
    clos()
}

funcExample(clos: exampleClos) // Closure is parameter now.

Trailing Closure Syntax 🤯

// Remove this 
let exampleClos = { 
    print("I'm actually parameter")
}

func funcExample(clos: () -> Void) {
    print("What?")
    clos()
}

funcExample(){
  print("I'm actually parameter")
}

// This part cannot be used when there is more than one parameter.
funcExample{
  print("I'm parameter")
}

// You can use it however you want.
// But I think it's complicated and reduces readability.

Tips 🤔💭

The training was too short today. I can't continue because of the rules. That's why I can't tip clues today.
If anyone is interested, I'll add the source of the day here.

Source is here Happy Coding 💻