[swift-evolution] Proposal: helpers for initializing properties of same name as parameters

David Waite david at alkaline-solutions.com
Sat Dec 5 20:24:05 CST 2015

A possible syntax, then:

	init(set name:String, set score:Int) { }

set would be a fourth parameter modifier alongside let, var, and inout - only valid on initializers (not as useful and likely confusing in other contexts). The local name has to match a parameter on the type. Like let/var (but unlike inout) usage of ‘set’ on a initializer parameter does not affect the caller or prototype conformance.


> On Dec 5, 2015, at 3:31 PM, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
>> Did you read through by reply to the original proposal?  I believe it provides the convenience you're looking for here while still allowing for the flexibility to avoid boilerplate in initializers that do more than just set properties.  
> I did, and I didn’t really like it. One of the syntaxes it proposed was strange and ad-hoc; the other was a fairly arbitrary overloading of parentheses, although it doesn’t actually conflict with anything since you can’t overload “calling parentheses” on a non-function.
> It’s important to ensure that, when the implementation evolves, you can replace the initializer transparently. For instance, suppose I have a type which represents a player in a game. It uses a stored property called `points`:

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 4139 bytes
Desc: not available
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151205/f7ed6ee1/attachment.p7s>

More information about the swift-evolution mailing list