Better Self support with class funcs


(James Campbell) #1

Would be great to construct the current class in a class function like so

class func makeMeCheese() -> Self
{
return Self(type: .Cheddar);
}

···

--
 Wizard
james@supmenow.com
+44 7523 279 698


#2

You can use `self` (and, to initialize, `self.init`) inside a class
function to refer to the class. A riff on your example here:
http://swiftstub.com/914697984/

Stephen

···

On Tue, Dec 15, 2015 at 6:56 AM, James Campbell via swift-evolution < swift-evolution@swift.org> wrote:

Would be great to construct the current class in a class function like so

class func makeMeCheese() -> Self
{
return Self(type: .Cheddar);
}

--
 Wizard
james@supmenow.com
+44 7523 279 698

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution


(James Campbell) #3

When doing this with a UIViewController's init nibname method, I get this
error:

Constructing and object of class type 'Self' with a metatype value must use
a 'required' initialiser.

···

On Tue, Dec 15, 2015 at 3:11 PM, Stephen Celis <stephen.celis@gmail.com> wrote:

You can use `self` (and, to initialize, `self.init`) inside a class
function to refer to the class. A riff on your example here:
http://swiftstub.com/914697984/

Stephen

On Tue, Dec 15, 2015 at 6:56 AM, James Campbell via swift-evolution < > swift-evolution@swift.org> wrote:

Would be great to construct the current class in a class function like so

class func makeMeCheese() -> Self
{
return Self(type: .Cheddar);
}

--
 Wizard
james@supmenow.com
+44 7523 279 698

_______________________________________________
swift-evolution mailing list
swift-evolution@swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution

--
 Wizard
james@supmenow.com
+44 7523 279 698


(Andrew Brown) #4

Seems to be ok in a playground - just needs a required initialiser.

class Base {

  class func create() -> Self {
    return self.init()
  }

  required init() {
  }

}

class Derived : Base {
}

let a = Base.create()
let b = Derived.create()