Last active
August 29, 2015 14:07
-
-
Save joshdholtz/8aec7bf4048e4235a95e to your computer and use it in GitHub Desktop.
Working Version
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
| // | |
| // ViewController.swift | |
| // SwiftTest | |
| // | |
| // Created by Josh Holtz on 8/3/14. | |
| // Copyright (c) 2014 Josh Holtz. All rights reserved. | |
| // | |
| import UIKit | |
| class ViewController: UIViewController { | |
| override func viewDidLoad() { | |
| super.viewDidLoad() | |
| var a = Maker<A>.make(); | |
| a.talk() // Outputs "Aaaaaa" | |
| var b = Maker<B>.make(); | |
| b.talk() // Outputs "Bbbbbb" | |
| var c = Maker<C>.make(); | |
| c.talk() // Outputs "Cccccc" | |
| var d = Maker<D>.make(); | |
| d.talk() // Outputs "Dddddd" | |
| } | |
| } | |
| class Maker<T:Base> { | |
| class func make() -> T { | |
| return T.create() | |
| } | |
| } | |
| protocol Base { | |
| init() | |
| func talk() | |
| class func create() -> Self; | |
| } | |
| class A: Base { | |
| required init() {} | |
| func talk() { | |
| println("Aaaaaa") | |
| } | |
| class func create() -> Self { return self() } | |
| } | |
| class B: Base { | |
| required init() {} | |
| func talk() { | |
| println("Bbbbbb") | |
| } | |
| class func create() -> Self { return self() } | |
| } | |
| class C: B { | |
| required init() { super.init() } | |
| override func talk() { | |
| println("Cccccc") | |
| } | |
| override class func create() -> Self { return self() } | |
| } | |
| class D: B, Base { | |
| required init() { super.init() } | |
| override func talk() { | |
| println("Dddddd") | |
| } | |
| override class func create() -> Self { return self() } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment