[swift-evolution] [Review] SE-0018 Flexible Memberwise Initialization
Tal Atlas
me at tal.by
Thu Jan 7 09:43:33 CST 2016
Overall I think this is a definite improvement to the current status quo
and something needs to be solved. My only concern with this specific
proposal is that on an initial glance it's not easy to intuit what the
behavior is. I like the end result of this proposal, and I think since it'd
be frequently used it'd be easy to remember what it was despite being
unintuitive.
I had originally suggested this alternative:
```
struct Foo {
let bar: String
let bas: Int
let baz: Double
init(self.bar: String, self.bas: Int, bax: Int) {
// self.bar = bar synthesized by the compiler
// self.bas = bas synthesized by the compiler
self.baz = Double(bax)
}
}
```
The upside of this is that it's more clear what happens, downside is that
adding a property requires 2 changes (as opposed to current 3, but more
than the 1 change required in proposal).
Perhaps a syntax more along the lines of this would be more intuitive and
still eliminate the boilerplate.
```
struct Foo {
let bar: String
let bas: Int
let baz: Double
init(bax: Int, self = ...) {
// self.bar = bar synthesized by the compiler
// self.bas = bas synthesized by the compiler
self.baz = Double(bax)
}
}
```
`memberwise` means nothing to the uninitiated, and `...` without any other
operator is a little confusing. I think this solves the issue of saying
what the `...` is doing.
On Thu, Jan 7, 2016 at 10:12 AM Matthew Johnson via swift-evolution <
swift-evolution at swift.org> wrote:
> On Jan 7, 2016, at 2:46 AM, Kevin Ballard via swift-evolution <
> swift-evolution at swift.org> wrote:
>
> * What is your evaluation of the proposal?
>
>
> +1 to the basic proposal. I'm much more reserved about the "Future
> enhancements"; some I don't want at all, some sound plausible but probably
> need changes.
>
>
> Thanks for your support Kevin!
>
> As I stated last night, there are two primary things that I think need
> improving via enhancements of some kind. I hope you can agree with these
> points:
>
> 1. We need a way to specify a default value for memberwise parameters for
> `let` properties.
> 2. We need a little bit more control over which properties participate in
> memberwise initialization when the “automatic” rules don’t quite do the
> right thing for a particular use case.
>
> Most of the enhancements listed show various ways we could address the
> second point. We don’t need all of them.
>
> Under the current proposal there will be cases where memberwise
> initialization would be of great use, but the rules of the automatic model
> expose a property (probably a `let`) that shouldn’t be exposed. We won’t
> be able to use memberwise initialization at all in those cases if we don’t
> have a way to correct that behavior.
>
> There may also be cases where a property doesn’t participate in memberwsie
> initialization when we would like it to. In those cases we can add an
> explicit parameter and manually initialize the property, continuing to use
> memberwise initialization for other properties.
>
> Ideally we can find a simple enhancement that solves both visibility
> problems. I’m definitely open to any ideas on how we can handle this.
>
>
> Also, a question and a concern about the basic proposal. The question: you
> state that the only impact this has on existing code is structs with
> private properties that have been getting an implicit internal memberwise
> initializer will have the initializer be private. That's fine, but assuming
> that the implicit memberwise initializer behaves identically to `memberwise
> init(...) {}`, surely this proposal also makes the implicit memberwise
> initializer gain defaulted arguments for any var properties with an
> initializer expression? Don't get me wrong, I think it's good to change
> that, but it should be explicitly noted.
>
>
> That is a great point! It won’t break any existing code, but it will
> change behavior slightly. I will update the proposal and submit a PR with
> this change.
>
>
> As for my concern, it's with the following rule:
>
> If the initializer body assigns to a var property that received memberwise
> initialization synthesis report a warning. It is unlikely that overwriting
> the value provided by the caller is the desired behavior.
>
>
> I understand why you put this in there, but this is a warning that cannot
> be suppressed and will make it impossible to use a memberwise initializer
> for perfectly legitimate cases where you do in fact want to mutate the
> property after it's been assigned to.
>
>
> For normal initializers I agree with you. However, I think it’s a
> reasonable for callers to assume that if you expose a property via
> memberwise initialization the post-initialization value will match the
> value they provide. This warning is intended to alert you to the fact that
> you are violating that reasonable assumption.
>
> Do you have an example of where you would want a caller to initialize a
> property, but then overwrite the value they provide *during
> initialization*?
>
>
> What might be a reasonable thing is a warning that occurs if you assign to
> the var property without ever having read from it (i.e. a dead store
> warning on the memberwise initialization of the property). That way if I
> mutate a property to contain a derived value it's fine, but if I simply
> write to it without ever reading it, it's a problem.
>
>
> * Is the problem being addressed significant enough to warrant a change to
> Swift?
>
>
> I think so. Writing initializers can be a large source of boilerplate in
> Swift, especially when using classes where you can't get the implicit
> memberwise initializer.
>
> * Does this proposal fit well with the feel and direction of Swift?
>
>
> Yes.
>
> * If you have you used other languages or libraries with a similar
> feature, how do you feel that this proposal compares to those?
>
>
> I'm not aware of a similar feature in any language I'm familiar with.
>
> * How much effort did you put into your review? A glance, a quick reading,
> or an in-depth study?
>
>
> I spent a few minutes reading over the whole proposal. I did not read any
> of the swift-evolution thread.
>
> -Kevin Ballard
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
>
> _______________________________________________
> 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/20160107/c433c8f1/attachment.html>
More information about the swift-evolution
mailing list