initialization - What's the use of expression.init() in Swift -
the swift programming language guide, page language reference -> expressions, section postfix expressions -> initializer expression talks syntax
<expression>.init(<initializer arguments>) when use (apart self.init() , super.init() covered in other parts of grammar)? seems me in case above expression valid (and expression not self or super), can do:
<expression>(<initializer arguments>) 
while syntax template looks this:
<expression>.init(<initializer arguments>) the grammar defines initializer-expression as:
initializer-expression → postfix-expression . init note invocation arguments not included. matches constructs a.init, , necessary because init keyword, a.init not match explicit-member-expression (which requires identifier rather keyword member name).
initializer expressions part of grammar because can invoke initializer belongs class not direct superclass of class writing (c invokes a.init in example):
class {     var property: string     init() {         property = "hello"     } }  class b: {     init() {         super.init()         property = "goodbye"     } }  class c: b {     init() {         super.init()         a.init()     } }  c().property // "hello" i don't think there many use cases this, both grammar , compiler allow , example compiles , runs correctly.
you can use initializer expression in global scope normal initializer. following 2 lines equivalent:
string(10) string.init(10) 
Comments
Post a Comment