Created
October 20, 2015 09:23
-
-
Save vendruscolo/a3ec8909f2b86f43d972 to your computer and use it in GitHub Desktop.
Type alias example
This file contains hidden or 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
//: Playground - noun: a place where people can play | |
struct Foo<T> { | |
typealias FooHandler = (data: T) -> Void | |
let bar: T | |
var handler: FooHandler? | |
init(bar: T) { | |
self.bar = bar | |
} | |
func doSomething() { | |
handler?(data: bar) | |
} | |
} | |
var baz = Foo(bar: 1) | |
baz.handler = { data in | |
print(data) // Prints 1 | |
} | |
baz.doSomething() | |
var foobarbaz = Foo(bar: "🍌") | |
foobarbaz.handler = { data in | |
print(data) // Prints "banana" | |
} | |
foobarbaz.doSomething() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment