Created
June 18, 2015 00:49
-
-
Save rnewman/3fb0c4dbd25e7fda7e3d to your computer and use it in GitHub Desktop.
Generic factory
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
| import UIKit | |
| class Foo { | |
| let xx: Int | |
| required init(x: Int) { // Remove 'required' to see the factory fail. | |
| self.xx = x | |
| } | |
| } | |
| class Bar: Foo { | |
| var yy: Int = 1 | |
| } | |
| func construct<T: Foo>(x: Int, t: T.Type) -> T { | |
| return T(x: x) | |
| } | |
| var bar = construct(12, Bar.self) | |
| bar is Bar // Unexpectedly false. | |
| println("Yes!") | |
| bar.yy = 9 // Fails on this line. | |
| println("No!") // Never runs. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment