[swift-users] Member wise initializer doesn't work with default-initialized const properties

Neil Faiman neil.swift at faiman.org
Mon May 16 16:37:23 CDT 2016


Using the default Swift with Xcode 7.3.1.

It appears that you cannot use the implicit memberwise initializer with a struct that has “let” properties with default values.

This works perfectly:

    struct CF {
        let prop: Int
    }
    let cf = CF(prop: 1)

But give the property a default value:

    struct CF {
        let prop: Int = 0
    }
    let cf = CF(prop: 1)

And now the compiler complains:

    error: argument passed to call that takes no arguments
    let cf = CF(prop: 1)
               ~~~~~~~^~

I don’t believe that the Apple _The Swift Programming Language_ mentions this restriction.

	Neil Faiman


More information about the swift-users mailing list