Add default value to inout parameter using Swfit
Asked Answered
A

3

5

In Swift 2 it is possible to do the following:

class SomeType {
    static let singletonInstance = SomeType()

    func someFunction(var mutableParameter: SomeType = SomeType.singletonInstance) {
        ...
    }
}

However in Swift 3 the var keyword will be removed for function parameters in favour of inout. I have not been able to achieve the same result as the above using the inout keyword.

class SomeType {
        static let singletonInstance = SomeType()

        func someFunction(inout mutableParameter: SomeType = SomeType.singletonInstance) {
            ...
        }
    }

Instead I receive an error of "Default argument value of type 'SomeType' cannot be converted to type 'inout SomeType'"

My question is whether it is possible to use inout with default value?

Amphisbaena answered 31/8, 2016 at 9:21 Comment(2)
Using var in the signature was not the same as using inout. It just made the argument locally variable.Egon
#36165473 should answer your question.Emad
D
6

The two keywords you're talking about, inout and var, are very different.

From Apple Documentation:

In-out parameters are passed as follows:

  1. When the function is called, the value of the argument is copied.
  2. In the body of the function, the copy is modified.
  3. When the function returns, the copy’s value is assigned to the original argument.

Therefore you can't give a default value to an inout parameter, as it would make the inout property completely useless.

What you can do is receive a normal (constant) parameter with a default value, and declare a new var with the same name this way (code from the Swift Evolution's Removing var from Function Parameters Proposal, with the addition of the default parameter):

func foo(i: Int = 5) {
    var i = i
    // now you can change i as you'd like
}
Debag answered 31/8, 2016 at 9:35 Comment(3)
If the argument i exists as a variable outside the function, and you would like the function to update it's value, as well as include a default value... how would you do that? Is it possible in Swift 3?Fossiliferous
If the variable i is within the function scope, then you don't need to pass it as a parameter at all! :) Please see example here: swift.sandbox.bluemix.net/#/repl/58b8e0b2f09a045fe29a515cDebag
If you must have a default value as well, what about just returning the new value? Example here: swift.sandbox.bluemix.net/#/repl/58b8e1ccf09a045fe29a515eDebag
P
1

Anyone who needs a default inout parameter may consider such solution:

class SomeType {
    static let singletonInstance = SomeType()

    func someFunction(inout mutableParameter: SomeType) {
        ...
    }

    // Declare function without 'mutableParameter':
    func someFunction() {
        someFunction(SomeType.singletonInstance)
    }
}

When you specify the default value for the parameter, the Swift compiler automatically generates a function without the parameter, for you. In this solution we do it manually.

Polka answered 1/3, 2018 at 6:26 Comment(0)
O
1

You could create a second function signature to "emulate" a default value

func foo() {
    var v: Int = 0 // your default value
    foo(bar: &v)
}

func foo(bar: inout Int) {
    // do stuff
}
Obstruent answered 6/12, 2020 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.