Created
August 13, 2020 09:38
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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