Skip to content

Fix Swift examples for value and reference types swiftlang issue #1268#1315

Open
mpapple-swift wants to merge 2 commits into
swiftlang:mainfrom
mpapple-swift:fix_issue_1268
Open

Fix Swift examples for value and reference types swiftlang issue #1268#1315
mpapple-swift wants to merge 2 commits into
swiftlang:mainfrom
mpapple-swift:fix_issue_1268

Conversation

@mpapple-swift
Copy link
Copy Markdown

Fix examples to clarify value and reference types in Swift to address issue #1268.

Change to value-and-reference-types.md to address a problem raised in swiftlang issue #1268

Motivation:

As mentioned in the associated swiftlang issue #1268, the 'reference' example is incorrect in that classes do not have an implicit initializer as is the case for structs.

Modifications:

As suggest in the issue, I modified the class Document definition to add an initializer and set the value of the String variable 'text' to an empty string.

I have added some additional boilerplate in the form of classes that further the example given. I am of the opinion that someone learning Swift from this article should be able to copy and paste the example code into Xcode or an IDE and have the code compile and run. However, the existing example code, while clearly demonstrating the difference between value and reference types, will fail to compile as the print functions will produce a "Expressions are not allowed at the top level" error. I am not wedded to these additions and will revert them to just modify the problem raised in issue #1268, but I hope you will consider the ergonomic aspect of providing code that compiles and runs as opposed to example code that merely demonstrates a point.

Result:

The modifications to value-and-reference-types.md will address the fact that the reference example with a class type presents an incorrect usage of class semantics.

Fix examples to clarify value and reference types in Swift to address issue swiftlang#1268.
@dempseyatgithub
Copy link
Copy Markdown
Contributor

First - thank you for taking this on and for your thoughtful comments.

With the addition of the missing init method required for the Document class, both code examples in the article run as-is in a Swift playground or at https://swiftfiddle.com.

I believe the proposed addition of the wrapper classes greatly reduces the clarity of the example code and also uses a pattern of performing non-initialization tasks in the init() methods of the wrapper classes.

The intent of the two samples is that two examples of almost identical and relatively easy to follow bits of code has two very different behaviors depending on whether the declared type is a struct or class.

I am not wedded to these additions and will revert them to just modify the problem raised in issue #1268

After considering the addition of the wrapper classes, I think it would work better to remove those changes and only add the one init() method to the Document class.

Copy link
Copy Markdown
Contributor

@dempseyatgithub dempseyatgithub left a comment

Choose a reason for hiding this comment

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

My rationale for requested changes is in the main PR discussion.

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.

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.

```
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")
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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants