Fix Swift examples for value and reference types swiftlang issue #1268#1315
Fix Swift examples for value and reference types swiftlang issue #1268#1315mpapple-swift wants to merge 2 commits into
Conversation
Fix examples to clarify value and reference types in Swift to address issue swiftlang#1268.
|
First - thank you for taking this on and for your thoughtful comments. With the addition of the missing init method required for the 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 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.
After considering the addition of the wrapper classes, I think it would work better to remove those changes and only add the one |
dempseyatgithub
left a comment
There was a problem hiding this comment.
My rationale for requested changes is in the main PR discussion.
| struct Document { | ||
| var text: String | ||
| } | ||
| ``` |
There was a problem hiding this comment.
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 = "" |
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
The missing init method - yes, please keep.
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 #1268Motivation:
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.