[swift-users] Convenience initializers in structs?
Chris McIntyre
nothingwasdelivered at gmail.com
Tue Jul 18 17:54:17 CDT 2017
Was it ever explained why the syntax is different?
--
Chris McIntyre
> On Jul 18, 2017, at 6:46 PM, Jens Persson via swift-users <swift-users at swift.org> wrote:
>
> That is not true. Structs can have delegating initializers but they cannot be marked with `convenience` (only the initializers of class types can).
>
> This is very clear from both the documentation and the compiler:
>
> (1) The Swift Programming language (Swift 4):
> "Swift defines two kinds of initializers for *class* types to help ensure all stored properties receive an initial value. These are known as designated initializers and convenience initializers."
>
> (2) The compiler:
>
> struct S {
> var a, b: Int
> init(_ a: Int, _ b: Int) {
> self.a = a
> self.b = b
> }
> convenience init(_ ab: Int) { // <-- Error
> self.init(ab, ab)
> }
> }
> The error message is:
> Delegating initializers in structs are not marked with 'convenience'
> The suggested fix is to remove the word convenience.
>
> Please reread my previous post, perform the steps I describe (looking at Quick Help, also try jump to definition for that `init` and you'll see it is marked with `convenience` even though it is in a struct), also look at the link to the documentation for the Int init, it too is marked with convenience, even though Int is a struct.
>
> /Jens
>
>
>> On Tue, Jul 18, 2017 at 9:46 PM, Slava Pestov <spestov at apple.com> wrote:
>> Hi Jens,
>>
>> While I’m not familiar with the integer API in the standard library, structs and enums certainly can have convenience initializers. They must delegate to another initializer (either convenience or designated) rather than initializing the fields of the type one by one.
>>
>> Slava
>>
>>> On Jul 18, 2017, at 6:46 AM, Jens Persson via swift-users <swift-users at swift.org> wrote:
>>>
>>> Start a command line project in Xcode 9 beta 3 and copy paste this single line of code into main.swift
>>>
>>> let _ = UInt8.init(extendingOrTruncating: UInt64(123456))
>>>
>>> Now look at Quick Help while placing the cursor on `init` and then on `extendingOrTruncating`.
>>>
>>> Note that (and how) the documentation for the initializer differs depending on where you place the cursor.
>>>
>>> If the cursor is on `init`, the initializer is shown to be a convenience(!) initializer even though structs (such as UInt8) cannot have convenience initializers, right?
>>>
>>> Even the official documentation for this and several other initializer like eg:
>>> https://developer.apple.com/documentation/swift/int/2885075-init
>>> clearly shows convenience initializers in structs.
>>>
>>> By the way, .init(extendingOrTruncating:) doesn't show in the list of completions for "UInt8.init" but it does for "UInt8(".
>>>
>>>
>>> Can anyone explain what's going on?
>>>
>>> Are these known issues that will go away in time for Xcode 9 GM?
>>>
>>> /Jens
>>>
>>> _______________________________________________
>>> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-users/attachments/20170718/f20ccedf/attachment.html>
More information about the swift-users
mailing list