[swift-evolution] [swift-evolution-announce] [Review] SE-0189: Restrict Cross-module Struct Initializers
Drew Crawford
drew at sealedabstract.com
Mon Nov 20 22:14:12 CST 2017
> How so? It seems that encapsulation is orthogonal to reference/value semantics.
Consider this type
public struct Mailbox {
private var _messages: [String]
public mutating func append(message: String) { _messages.append(message)}
public var messages: [String] { return _messages}
public var sortedMessages: [String] { return _messages.sorted() }
}
We subsequently discover in the wild that although there are a variety of client patterns, certain "important" clients have the particular pattern
1. Add messages up front
2. Use sortedMessages accessor frequently and exclusively
It would improve the important clients without harming other clients to use the following implementation for our type: if the sorted accessor is used before the unsorted one, sort the underlying storage, and then serve that until the type is subsequently mutated (which important clients do not do). This amortizes the cost of the sort across many calls to the accessor.
Although the underlying storage is "private" and we may consider its sorted-arity an implementation detail of Mailbox, in fact there is no straightforward way to get to that implementation from this one. When we chose value semantics we promised clients that our "encapsulated" values will not change inside a getter; mutating the "hidden" storage breaks our promise.
I would argue this "encapsulation failure" of the _messages array is actually a feature, and part of the draw of value types is that they provide these kinds of guarantees to clients, and we subvert them at our peril. In cases where that is not desired reference types are available.
> We want to be able to add new stored properties to structs, or change existing stored properties to computed properties (and vice versa) without breaking binary or source compatibility.
If CGAffineTransform, CGPoint, in_addr_t etc. are changing their members we have a problem. There are probably structs where this makes more sense, but I can't immediately think of an example.
On November 20, 2017 at 6:16:55 PM, Slava Pestov (spestov at apple.com) wrote:
On Nov 20, 2017, at 1:58 PM, Drew Crawford via swift-evolution <swift-evolution at swift.org> wrote:
I'm "weak oppose" on this proposal.
The core premise here is to increase the encapsulation of a struct around its member variables. But I think the purview of encapsulation is more classes than structs.
How so? It seems that encapsulation is orthogonal to reference/value semantics.
e.g. a struct leaks information about the mutation of its member variables, even if those variables are private.
Can you explain what you mean by this?
Structs are the obvious implementation for a named tuple (CGPoint CGAffineTransform UIColor etc.) where there is inherently a fixed set of members that are more conveniently accessed directly. Structs and classes have different purposes and so the argument for consistency with classes is weak.
With regard to the BalancedPair problem, I would prefer to see a "final struct" or "required init”.
The real reason we want to introduce this language change is to solve some problems related to resilience. We want to be able to add new stored properties to structs, or change existing stored properties to computed properties (and vice versa) without breaking binary or source compatibility. Since non-delegating initializers expose the exact set of stored properties to the client module, they break the encapsulation that we need to allow this.
Slava
On November 14, 2017 at 1:31:25 PM, Ted Kremenek (kremenek at apple.com) wrote:
The review of "SE-0189: Restrict Cross-module Struct Initializers" begins now and runs through November 21, 2017.
The proposal is available here:
https://github.com/apple/swift-evolution/blob/master/proposals/0189-restrict-cross-module-struct-initializers.md
Reviews are an important part of the Swift evolution process. All review feedback should be sent to the swift-evolution mailing list at:
https://lists.swift.org/mailman/listinfo/swift-evolution
or, if you would like to keep your feedback private, directly to the review manager.
When replying, please try to keep the proposal link at the top of the message:
Proposal link: https://github.com/apple/swift-evolution/blob/master/proposals/0189-restrict-cross-module-struct-initializers.md
...
Reply text
...
Other replies
What goes into a review of a proposal?
The goal of the review process is to improve the proposal under review through constructive criticism and, eventually, determine the direction of Swift.
When reviewing a proposal, here are some questions to consider:
What is your evaluation of the proposal?
Is the problem being addressed significant enough to warrant a change to Swift?
Does this proposal fit well with the feel and direction of Swift?
If you have used other languages or libraries with a similar feature, how do you feel that this proposal compares to those?
How much effort did you put into your review? A glance, a quick reading, or an in-depth study?
Thanks,
Ted Kremenek
Review Manager
_______________________________________________
swift-evolution-announce mailing list
swift-evolution-announce at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution-announce
_______________________________________________
swift-evolution mailing list
swift-evolution at swift.org
https://lists.swift.org/mailman/listinfo/swift-evolution
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20171120/6d655331/attachment-0001.html>
More information about the swift-evolution
mailing list