[swift-evolution] Reconsidering SE-0003 Removing var from Function Parameters and Pattern Matching
Taras Zakharko
taras.zakharko at uzh.ch
Mon Jan 25 08:13:57 CST 2016
The real issue here is the contrast between reference and value types. What you suggest is basically to introduce a new type to the language: references to values. This type already exists implicitly for the inout parameters. In more simple terms, your example turns into:
if var array = &someStruct.someOptionalArrayl {
array.append(foo)
}
One could expose reference types to the general language instead of keeping it specific to inout arguments only. It might make the language more elegant sometimes. But it might also make it more messy, as it allows one to blur the reference/value distinction deliberately. I really don’t have any opinion right now because I have no idea of what the consequences would be.
But I am certainly against having an ‘inout’ variable declaration modifier. If that is really necessary, than I’d rather see a general reference implementation.
Best,
Taras
> On 25 Jan 2016, at 13:12, Charles Srstka via swift-evolution <swift-evolution at swift.org> wrote:
>
>> On Jan 25, 2016, at 3:54 AM, Quinn The Eskimo! via swift-evolution <swift-evolution at swift.org> wrote:
>>
>>
>> On 25 Jan 2016, at 03:25, Dave Abrahams via swift-evolution <swift-evolution at swift.org> wrote:
>>
>>> On 25 Jan 2016, at 01:44, Charles Srstka via swift-evolution <swift-evolution at swift.org> wrote:
>>>
>>>> But that’s not what assigning something to a var means in any other context. Why should it be any different here?
>>>
>>
>>> I made the same argument myself, but you can't argue with empirical
>>> evidence: some people have been tripped up by this anyway.
>>
>> One explanation might be that these folks have a Pascal heritage, where "var" is (roughly) equivalent to Swift's "inout”.
>
> Perhaps then we should add an “if inout” construct, then, so that the distinction between “inout” and “var” here are clear for people to see, and explained in the documentation?
>
> As a bonus, I can think of a few situations where “if inout” would be useful—particularly so we wouldn’t need the postfix ! to do things like this:
>
> if someStruct.someOptionalArray != nil {
> someStruct.someOptionalArray!.append(foo)
> }
>
> With “if inout”, you could do this instead:
>
> if inout array = someStruct.someOptionalArray {
> array.append(foo)
> }
>
> which seems quite a bit cleaner.
>
> Charles
>
> _______________________________________________
> 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