[swift-evolution] thoughts on 'inout'

Haravikk swift-evolution at haravikk.me
Thu Feb 11 17:47:39 CST 2016


> On 11 Feb 2016, at 23:29, Brent Royal-Gordon via swift-evolution <swift-evolution at swift.org> wrote:
> 
>> My gut reaction to typing 'inout' in front of every parameter I want to call by reference is that it makes code look rather waffly.
>> 
>> So I'm reflecting an idea on this:
>> 
>> 
>> Use = to assign values.
>> 
>> Use : to associate variables.
>> 
>> 
>> 
>> A function declaration would look like this:
>> 
>> func addInt(add intToAdd = Int, to counterInt : Int ) {
>>    counterInt += intToAdd
>> }
> 
> This doesn't work with unlabeled parameters. I think it also makes inout parameters too invisible; they have unusual semantics and ought to be marked. I don't think it's a good idea to overload `=` for parameter passing; it requires different spacing to look right, and the change would alter literally every method call in existing code. `=` is also semantically heavier than `:`, and yet it's being used in the lighter role here. And I think it makes inout parameters too "easy"—they're a pretty rare case and it's okay to require more than zero extra characters to declare them.
> 
> In short, -1.

It’s a nice idea, but I have to agree with Brent; I think that while it’s concise, it’s not necessarily clear, especially if a function may have both inout parameters and parameters with default values. Also I tend to favour a little bit of extra verbosity, especially on things that may not be used a lot (depends on what you’re doing of course); while inout isn’t my favourite term it’s at least fairly clear of what it’s for to someone encountering it for the first time, though I might have gone with reference, or ref (in keeping with the three letter var and let) but that may just be me.


More information about the swift-evolution mailing list