Question: I am extending a base class (one which I do not control) in Swift. I want to provide a class function for creating an instance typed to a subclass. A generic function is required. However, an implementation like the one below does not return the expected subclass type. 

```
class Calculator {
    func showKind() { println("regular") }
}

class ScientificCalculator: Calculator {
    let model: String = "HP-15C"
    override func showKind() { println("scientific") }
}

extension Calculator {
    class func create<T:Calculator>() -> T {
        let instance = T()
        return instance
    }
}

let sci: ScientificCalculator = ScientificCalculator.create()
sci.showKind()

```

The debugger reports `T` as `ScientificCalculator`, but `sci` is `Calculator` and calling `sci.showKind()` returns "regular". 

Is there a way to achieve the desired result using generics, or is it a bug?

Answer: 
Ok, from the [developer forums](https://devforums.apple.com/message/1064957#1064957), if you have control of the base class, you might be able to implement the following work around.

```
class Calculator {
    func showKind() { println("regular") }
    required init() {}
}

class ScientificCalculator: Calculator {
    let model: String = "HP-15C"
    override func showKind() { println("\(model) - Scientific") }
    required init() {
        super.init()
    }
}

extension Calculator {
    class func create<T:Calculator>() -> T {
        let klass: T.Type = T.self
        return klass()
    }
}

let sci:ScientificCalculator = ScientificCalculator.create()
sci.showKind()

```

Unfortunately if you do not have control of the base class, this approach is not possible.
Update, i used Calculator as return type which makes no sense. It should be Self, but still a required init has to exist:

```
class Calculator {
    func showKind() { println("regular") }
    required init() {

    }
}

class ScientificCalculator: Calculator {
    let model: String = "HP-15C"
    override func showKind() { println("scientific") }
}

extension Calculator {
    class func create() -> Self {
        return self()
    }
}

var b = ScientificCalculator.create() // HP-15C
b.showKind()

```

---

I addition to Sean Woodward, it will work if Calculators have (required) initializers. Overriding that in a subclass is not needed.

```
class Calculator {
    required init() {

    }
    func showKind() { println("regular") }
}

class ScientificCalculator: Calculator {
    let model: String = "HP-15C"
    override func showKind() { println("scientific") }
}

extension Calculator {
    class func create<T:Calculator>( type:T.Type ) -> T {
        return type() as T
    }
    class func newInstance() -> Calculator {
        return self()
    }
}

var b = ScientificCalculator.newInstance() // HP-15C
b.showKind() // sientific

```