[swift-users] Needing self when one initialiser method calls another.

Trent Nadeau tanadeau at gmail.com
Sat Jan 16 10:44:05 CST 2016


Note that the compiler has a poor diagnostic for this case. I created
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 at 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 {
>
>
>     var x: Double
>     var y: Double
>
>
>     init() {
>         x = 0
>         y = 0
>     }
>
>
>     init(x: Double, y: Double) {
>         self.x = y
>         self.y = y
>     }
>
> }
>
>
> Designer . Developer .  Nerd
> Jo Albright
>
>
> On Jan 14, 2016, at 11:48 PM, Diego Barros via swift-users <
> swift-users at 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 at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>
>
> _______________________________________________
> swift-users mailing list
> swift-users at swift.org
> https://lists.swift.org/mailman/listinfo/swift-users
>
>


-- 
Trent Nadeau
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20160116/3c3d697e/attachment.html>


More information about the swift-users mailing list