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

Dan Appel dan.appel00 at gmail.com
Fri Dec 4 21:05:26 CST 2015


I'm not sure how I feel about this, but in either case, why limit it to
default parameters? It's useful in other situations, too.

Take this for example:
struct Foo {
    let bar: String
    let bas: Int
    let baz: Double
    init(self.bar: String, self.bas: Int, bax: Int) {
        self.baz = Double(bax)
    }
}

where the need to say

self.bar = bar
self.bas = bas

is now avoided.

On Fri, Dec 4, 2015 at 3:16 PM Tal Atlas <me at tal.by> wrote:

> There’s lots of boilerplate of initializing structs with stored properties
> and initializer parameters. I’d like to create a syntax for alleviating
> that in the 90% case.
>
> ```swift
> struct Foo {
>     let bar: String
>     let baz: Int
>
>     init(self.bar: String = "default", counter self.baz: Int) {
>     }
> }
> ```
>
> This would be identical to:
> ```swift
> struct Foo {
>     let bar: String
>     let baz: Int
>
>     init(bar: String = "default", counter baz: Int) {
>         self.bar = bar
>         self.baz = baz
>     }
> }
> ```
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>
-- 
Dan Appel
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20151205/901ade23/attachment.html>


More information about the swift-evolution mailing list