Skip to content

Instantly share code, notes, and snippets.

@rnewman
Created June 18, 2015 00:49
Show Gist options
  • Select an option

  • Save rnewman/3fb0c4dbd25e7fda7e3d to your computer and use it in GitHub Desktop.

Select an option

Save rnewman/3fb0c4dbd25e7fda7e3d to your computer and use it in GitHub Desktop.
Generic factory
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