Skip to content

Instantly share code, notes, and snippets.

@Gagan5278
Created August 13, 2020 09:38
Show Gist options
  • Save Gagan5278/8f53626aa778a2897b66f8f002a66623 to your computer and use it in GitHub Desktop.
Save Gagan5278/8f53626aa778a2897b66f8f002a66623 to your computer and use it in GitHub Desktop.
An awesome code to Create a class instance (without a Class) from a function in Swift
typealias Balance = (add: ((Int) -> Void), get: (() -> Int), print: () -> ()) //Tupple. Tupple is value type but inside tupple we have function with referenc type.
func newBalance() -> Balance {
var balance = 0
return (
add: { newValue in balance += newValue },
get: { return balance },
print: { print("Your balance is \(balance)") }
)
}
let balance = newBalance()
balance.add(3) // add 3
balance.get() // get 3
balance.print() // prints the balance
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment