Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why it is called the Memberwise Initialiser

Quote from the office Swift Document

All structure have an automatically-generated memberwise initializer, which you can use to initialize the member properties of new structure instances ...

Question 1: What is so special about the default initializer, why can't it be simply called the default initializer? Why adding the "memberwise"? Is it because it lists out all the member properties defined in the Structure. And you also have to follow the order defined inside the Structure when creating an instance.

Question 2 Is there any other special initializers who have their own special name? If so, what they look like.

[Note Part I:] For further discuss with Mr. Vadian

enter image description here

[Note Part II:]

enter image description here

like image 472
SLN Avatar asked Mar 11 '23 21:03

SLN


2 Answers

Regarding question 1:

There is an irrevocable law in Swift:

Classes and structures must set all of their stored properties to an appropriate initial value by the time an instance of that class or structure is created. Stored properties cannot be left in an indeterminate state.

We are talking about structs:

When creating a struct you can use the default initializer (the pair of parentheses) if all properties have a default value.

If you just declare the properties without a default value, the compiler creates an implicit memberwise initializer – which you have to use – to make sure to assign a default value to each property in a very convenient way

like image 50
vadian Avatar answered Mar 31 '23 12:03

vadian


This seems primarily opinion based, and not a good fit for SO, but here's my 2 cents:

  1. Default sounds a lot like Designated, which is a completely orthogonal concept.

  2. Not "special", but there are at least 2 different kinds of initializers: designated and convenience.

like image 32
Alexander Avatar answered Mar 31 '23 11:03

Alexander