Skip to content

Conversation

@mpapple-swift
Copy link

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.
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.

1 participant