2

Swift provide Memberwise Initializers for struct Types. But when it comes to class we need to explicitly provide an init() by ourselves. I was trying to understand logic behind such a decision.

Sample,

struct SomeStruct {
    var someVar:Int
}
// Memberwise Initializers
let structInstance = SomeStruct(someVar:0)

class SomeClass {
    var someVar:Int
}

let classInstance = SomeClass()

~~> ERROR at line 11, col 21: 'SomeClass' cannot be constructed because it has no accessible initializers let classInstance = SomeClass()

0

2 Answers 2

6

The logic behind it could be because a struct is not subclassable, making it obvious that a complete initializer would be required, and as such it was made implicit by the language.

On the other hand, a class is subclassable, and as such you may want to design it so that it can only be initialized from subclasses, and as such an implicit initializer was not provided by the language.

Sign up to request clarification or add additional context in comments.

Comments

2

(As pointed out by @Cœur below, this does not really answer the question, but might bring value to the context of it, so I'll leave it up)

From The Language Guide - Classes and Structures [emphasis mine]:

Definition syntax

Here’s an example of a structure definition and a class definition:

struct Resolution {
    var width = 0
    var height = 0
}
// ...

...

Memberwise Initializers for Structure Types

All structures have an automatically-generated memberwise initializer, which you can use to initialize the member properties of new structure instances. Initial values for the properties of the new instance can be passed to the memberwise initializer by name:

let vga = Resolution(width: 640, height: 480) 

Unlike structures, class instances do not receive a default memberwise initializer.

...

Also worth mentioning that these are only available given that the strucutre do not define any of their own custom initializers; from the Language Guide - Initialization

Memberwise Initializers for Structure Types

Structure types automatically receive a memberwise initializer if they do not define any of their own custom initializers. Unlike a default initializer, the structure receives a memberwise initializer even if it has stored properties that do not have default values.

...

6 Comments

doesn't answer the question, which was a why. :)
@Cœur But it does answer the question. "All structures have an automatically-generated memberwise initializer".
@Cœur I agree, reading the question more carefully. Nonetheless, Hamish found an appropriate dupe target for this thread, so I believe it should be closed as a duplicate (and I also believe both answers here bring value to SO readers that might bounce into this Q&A, as well as further into its dupe target).
@Cœur Exactly, It doesn't answer 'WHY'. But your following explanation make sense. "On the other hand, a class is subclassable, and as such you may want to design it so that it can only be initialized from subclasses, and as such an implicit initializer was not provided by the language." 👍
@Jinto just wondering if your next question will be: why, unlike class, struct is not subclassable? :joking:
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.