Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 29 additions & 12 deletions documentation/articles/value-and-reference-types.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,21 @@ Since it is an independent instance, changing the `text` of `friendDoc` does not
struct Document {
var text: String
}
```
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in my comment on the PR itself, please revert this section back to the original without the UseStructDocument type.

Example
```swift
class UseStructDocument {
var myDoc = Document(text: "Great new article")

var myDoc = Document(text: "Great new article")
var friendDoc = myDoc
init() {
var friendDoc = myDoc

friendDoc.text = "Blah blah blah"
friendDoc.text = "Blah blah blah"

print(friendDoc.text) // prints "Blah blah blah"
print(myDoc.text) // prints "Great new article"
print(friendDoc.text) // prints "Blah blah blah"
print(myDoc.text) // prints "Great new article"
}
}
```

When you send your friend a copy of a document, you are in complete control of when *your* copy changes. You never have to worry about your friend making some unexpected change to your copy of the document.
Expand All @@ -73,16 +80,26 @@ Since it is a reference to the same instance, changing the `text` of `friendDoc`

```swift
class Document {
var text: String
}
var text: String = ""
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the added init method takes and sets the text property, there is no need to initialize it here. Please revert this.


var myDoc = Document(text: "Great new article")
var friendDoc = myDoc
init(text: String) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The missing init method - yes, please keep.

self.text = text
}
}
```
Example
```swift
class UseClassDocument {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned in my comment on the PR itself, please revert this section back to the original without the UseClassDocument type.

var myDoc = Document(text: "Great new article")

friendDoc.text = "Blah blah blah"
init() {
let friendDoc = myDoc
friendDoc.text = "Blah blah blah"

print(friendDoc.text) // prints "Blah blah blah"
print(myDoc.text) // prints "Blah blah blah"
print(friendDoc.text) // prints "Blah blah blah"
print(myDoc.text) // prints "Blah blah blah"
}
}
```

When you send your friend a link to a shared document, your friend can make changes to the document without you knowing about it. You might be relying on your document staying the same.
Expand Down