[swift-evolution] Proposal: Keep var on Function Parameters and Pattern Matching

Myles La Verne Schultz myleslschultz at icloud.com
Mon Feb 8 19:10:31 CST 2016


I very humbly agree with Francisco.  I feel that providing a warning when the var function parameter is not mutated or returned is a better option than cutting out the functionality entirely.  i don’t think that that a random mistake here or there constitutes removal of a feature.  Especially when Swift is a language that has redefined and continues to redefine what we expect from a modern language.

In light of that fact, I like to think of Swift’s functions/methods as a type that I have defined.  When I define a type I provide properties and I can use those properties in functions within that type.  A function’s parameter list, to me, is like a headline of the properties contained in that type.  And just like making a struct or class, some of those properties are constant, and some are variable.  The inout “property” of a function then is the odd one in that it’s like have a pointer to some other type’s property. Thus,

struct SomeType {

    let prop1: Type1    //  Two properties, one constant and one variable
    var prop2: Type2

    func someFunction(arg1: Type1, var arg2: Type, inout: Type2) {

        //  Do stuff

    }

}

Now, to make a point, I’ll pull out the function and declare it like a type (because in Swift functions are types).

func someFunction {

    let arg1:  Type1
    var arg2: Type2
    inout arg3: Type2  //  This “property” can, in a sense, point to prop1 or prop2 from SomeType to mutate that value

    //  Do stuff, declare functions, etc.

}

I realize the example is rather contrived and has it’s problems, but I hope it makes the point that if you think about let, var, and inout in this way, it’s not confusing at all.  I would also argue that this IS the way Swift begs you think about it’s function signatures.  Just like making a new instance of a type, like SomeType above, where you make a new copy of it, calling someFunction is like creating a new type with it’s own properties whose values are defined through the parameter list.  If there is confusion on this, then I think that calls for clearer documentation not deleting functionality.

Even if it only deletes one line of code

func someFunction(arg1: Type1, arg2: Type, inout: Type2) {

    var arg2 = arg2  //  This line of code

    …

}

I think, it is completely worth it!  It helps us rethink how we think about functions in Swift.  It helps us remember they are types that come with a list of properties we know we’re going to need to complete the process we are about to declare.  Capturing a list of values so that you can mutate them within a function is "so C, so last decade” and just not necessary with Swift.

Again, I very humbly ask that the proposal to remove var from function parameter lists be revoke entirely though I do I understand it had previously already been acceptance.  Thank you your consideration.

Myles


More information about the swift-evolution mailing list