# Needing self when one initialiser method calls another.

**URL:** <https://forums.swift.org/t/needing-self-when-one-initialiser-method-calls-another/1085>\
**Category:** Using Swift\
**Created:** [January 15, 2016, 4:48am UTC](https://forums.swift.org/t/needing-self-when-one-initialiser-method-calls-another/1085 "2016-01-15T04:48:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Diego\_Barros](https://avatars.discourse-cdn.com/v4/letter/d/7ab992/32.png) [@Diego\_Barros](https://forums.swift.org/u/Diego_Barros)\
**Post date:** [January 15, 2016, 4:48am UTC](https://forums.swift.org/t/needing-self-when-one-initialiser-method-calls-another/1085/1 "2016-01-15T04:48:00Z")

</div>

Hi all,

​Given the ​following example:

struct Vector {

var x: Double

var y: Double

init() {

\*self.init(x: 0, y: 0)\*

}

init(x: Double, y: Double) {

self.x = y

self.y = y

}

}

I'm just curious as to why the \*self\* (in bold) is needed in the init()  
that's calling init(x: Double, y: Double)? Can't the compiler infer from  
the parameters what should be called?

- Diego

---

<div class="post-metadata">

**Author:** ![Jo\_Albright1](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@Jo\_Albright1](https://forums.swift.org/u/Jo_Albright1)\
**Post date:** [January 15, 2016, 6:35am UTC](https://forums.swift.org/t/needing-self-when-one-initialiser-method-calls-another/1085/2 "2016-01-15T06:35:14Z")

</div>

Hi Diego,

Since you have not given the x & y variables initial values, it requires them to be set inside the init. You can write it like below instead of calling the other self.init

struct Vector {  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;var x: Double  
&nbsp;&nbsp;&nbsp;&nbsp;var y: Double  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;init() {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 0  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = 0  
&nbsp;&nbsp;&nbsp;&nbsp;}  
&nbsp;&nbsp;&nbsp;&nbsp;  
&nbsp;&nbsp;&nbsp;&nbsp;init(x: Double, y: Double) {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.x = y  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.y = y  
&nbsp;&nbsp;&nbsp;&nbsp;}

}

Designer . Developer .  Nerd  
Jo Albright

> **···**
>
> > On Jan 14, 2016, at 11:48 PM, Diego Barros via swift-users \<swift-users@swift.org\> wrote:
> > 
> > Hi all,
> > 
> > ​Given the ​following example:
> > 
> > struct Vector {  
> > &nbsp;&nbsp;var x: Double  
> > &nbsp;&nbsp;var y: Double  
> > &nbsp;&nbsp;  
> > &nbsp;&nbsp;init() {  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.init(x: 0, y: 0)  
> > &nbsp;&nbsp;}  
> > &nbsp;&nbsp;  
> > &nbsp;&nbsp;init(x: Double, y: Double) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.x = y  
> > &nbsp;&nbsp;&nbsp;&nbsp;self.y = y  
> > &nbsp;&nbsp;}  
> > }
> > 
> > I'm just curious as to why the self (in bold) is needed in the init() that's calling init(x: Double, y: Double)? Can't the compiler infer from the parameters what should be called?
> > 
> > - Diego  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)

---

<div class="post-metadata">

**Author:** ![Trent\_Nadeau](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/trent_nadeau/32/806_2.png) [@Trent\_Nadeau](https://forums.swift.org/u/Trent_Nadeau)\
**Post date:** [January 16, 2016, 4:44pm UTC](https://forums.swift.org/t/needing-self-when-one-initialiser-method-calls-another/1085/3 "2016-01-16T16:44:05Z")

</div>

Note that the compiler has a poor diagnostic for this case. I created  
[[SR-430] Poor diagnostic when using delegating initializers without self · Issue #43047 · apple/swift · GitHub](https://bugs.swift.org/browse/SR-430) a few weeks ago when I found this  
issue myself.

> **···**
>
> On Fri, Jan 15, 2016 at 1:35 AM, Jo Albright via swift-users \< swift-users@swift.org\> wrote:
> 
> > Hi Diego,
> > 
> > Since you have not given the x & y variables initial values, it requires  
> > them to be set inside the init. You can write it like below instead of  
> > calling the other self.init
> > 
> > struct Vector {
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;var x: Double  
> > &nbsp;&nbsp;&nbsp;&nbsp;var y: Double
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;init() {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;x = 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;y = 0  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > &nbsp;&nbsp;&nbsp;&nbsp;init(x: Double, y: Double) {  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.x = y  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;self.y = y  
> > &nbsp;&nbsp;&nbsp;&nbsp;}
> > 
> > }
> > 
> > Designer . Developer .  Nerd  
> > Jo Albright
> > 
> > On Jan 14, 2016, at 11:48 PM, Diego Barros via swift-users \< \> swift-users@swift.org\> wrote:
> > 
> > Hi all,
> > 
> > ​Given the ​following example:
> > 
> > struct Vector {  
> > var x: Double  
> > var y: Double
> > 
> > init() {  
> > \*self.init(x: 0, y: 0)\*  
> > }
> > 
> > init(x: Double, y: Double) {  
> > self.x = y  
> > self.y = y  
> > }  
> > }
> > 
> > I'm just curious as to why the \*self\* (in bold) is needed in the init()  
> > that's calling init(x: Double, y: Double)? Can't the compiler infer from  
> > the parameters what should be called?
> > 
> > - Diego  
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> > 
> > \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_  
> > swift-users mailing list  
> > swift-users@swift.org  
> > [https://lists.swift.org/mailman/listinfo/swift-users](https://lists.swift.org/mailman/listinfo/swift-users)
> 
> --  
> Trent Nadeau

---

<div class="post-metadata">

**Author:** ![Brent\_Royal-Gordon](https://avatars.discourse-cdn.com/v4/letter/b/53a042/32.png) [@Brent\_Royal-Gordon](https://forums.swift.org/u/Brent_Royal-Gordon)\
**Post date:** [January 17, 2016, 12:23am UTC](https://forums.swift.org/t/needing-self-when-one-initialiser-method-calls-another/1085/4 "2016-01-17T00:23:28Z")

</div>

> I'm just curious as to why the self (in bold) is needed in the init() that's calling init(x: Double, y: Double)? Can't the compiler infer from the parameters what should be called?

My interpretation: The `self` must be explicit here to differentiate it from `super.init`. You can't use `super.init` in a struct, of course, but in a class this is a super, super crucial distinction, so it's helpful for the compiler to force you to be clear about this in a class. Structs have the same rule simply because it would be more frustrating if it were inconsistent.

> **···**
>
> --  
> Brent Royal-Gordon  
> Architechies

---

<div class="post-metadata">

**Author:** ![Diego\_Barros](https://avatars.discourse-cdn.com/v4/letter/d/7ab992/32.png) [@Diego\_Barros](https://forums.swift.org/u/Diego_Barros)\
**Post date:** [January 17, 2016, 5:32am UTC](https://forums.swift.org/t/needing-self-when-one-initialiser-method-calls-another/1085/5 "2016-01-17T05:32:16Z")

</div>

The existence of `super` in classes makes sense as to why it's required.

-- diego

> **···**
>
> On 17 January 2016 at 11:23, Brent Royal-Gordon \<brent@architechies.com\> wrote:
> 
> > \> I'm just curious as to why the self (in bold) is needed in the init()  
> > that's calling init(x: Double, y: Double)? Can't the compiler infer from  
> > the parameters what should be called?
> > 
> > My interpretation: The `self` must be explicit here to differentiate it  
> > from `super.init`. You can't use `super.init` in a struct, of course, but  
> > in a class this is a super, super crucial distinction, so it's helpful for  
> > the compiler to force you to be clear about this in a class. Structs have  
> > the same rule simply because it would be more frustrating if it were  
> > inconsistent.
> > 
> > --  
> > Brent Royal-Gordon  
> > Architechies
