[swift-evolution] [Pitch] Unifying init parameters with properties

Vladimir.S svabox at gmail.com
Mon Apr 18 07:37:20 CDT 2016


-1 on this. After the parameter declaration we expect to see a type of that 
parameter. I don't think we should break Swift method declarations for this 
feature. I prefer to see information about parameter types in func/init 
declaration. I don't want to scroll up to the start of class declaration to 
find out the type of parameter.

And, why in this case we need these foo: bar: at all?
We had a discussion earlier in this thread about such variant:

class Foo
{
let foo : String
let bar : String
let barCount : Int
let baz : Int

init(self.foo, self.bar, self.baz) // just self.foo etc, no parameter 
labels at ll
{
self.barCount = bar.characters.count
}
}

But I also think this is not OK to not have a types of parameters in init.

On 16.04.2016 16:17, Jonathan Hull via swift-evolution wrote:
> Since we know the types of the properties, how about we replace the type in
> the signature with either an indication that the property should be
> automatically set, or better yet, the property which should be set:
>
> class Foo
> {
> let foo : String
> let bar : String
> let barCount : Int
> let baz : Int
>
> init(foo: self.foo, bar: self.bar, baz: self.baz)
> {
> self.barCount = bar.characters.count
> }
> }
>
> That way you don’t always have to have the init’s parameter names match the
> names of the properties they set (even though they often would).  We could
> also allow a leading dot as a shorthand for ‘self.’
>
> init(foo: .foo, bar: .bar, baz: .baz)
>
> I think I like explicit ‘self.’ better, but that may just be my preference.
>  In either case, the generated interface would show the actual type.
>
> // init(foo: String, bar: String, baz: Int)
>
> Thanks,
> Jon
>
>> This is a common pattern for initialisers at the moment:
>>
>> class Foo
>>
>> {
>>
>> let foo : String
>>
>> let bar : String
>>
>> let barCount : Int
>>
>> let baz : Int
>>
>>
>> init(foo: String, bar: String, baz: Int)
>>
>> {
>>
>> self.foo = foo
>>
>> self.bar = bar
>>
>> self.baz = baz
>>
>> barCount = bar.characters.count
>>
>> }
>>
>> }
>>
>> This involves a lot of using 'self.'. For those who prefer not to use
>> 'self.' explicitly everywhere, this is probably the main place it gets
>> used. It's a lot of boilerplate code.
>>
>> How would it be if, like default variables, we could pack some of that
>> information into the argument tuple, and unify parameters with properties
>> immediately?
>>
>> class Foo
>>
>> {
>>
>> let foo : String
>>
>> let bar : String
>>
>> let barCount : Int
>>
>> let baz : Int
>>
>>
>> init(self.foo: String, self.bar: String, self.baz: Int)
>>
>> {
>>
>> barCount = bar.characters.count
>>
>> }
>>
>> }
>>
>> Less boilerplate, more focus on the properties which need to be generated.
>>
>> Thoughts?
>
>
>
> _______________________________________________
> swift-evolution mailing list
> swift-evolution at swift.org
> https://lists.swift.org/mailman/listinfo/swift-evolution
>


More information about the swift-evolution mailing list