Edward Alric
Edward Alric

Reputation: 61

"implicit conversion from <tuple type> to <tuple type 2> requires a temporary" error when passing a tuple as an inout argument

This is my code:

var myTuple = ("bar", 42)

func foo(_ bar: inout (arg1: String, arg2: Double)) {
    [...]
}

foo(&myTuple)

I get the following error for this line:

foo(&myTuple)

Cannot pass immutable value as inout argument: implicit conversion from '(String, Double)' to '(arg1: String, arg2: Double)' requires a temporary

Upvotes: 2

Views: 441

Answers (2)

glyvox
glyvox

Reputation: 58029

The actual problem is that your tuple variable is missing labels that are present in the function. Replace it with the following:

var myTuple = (arg1: "bar", arg2: 42)

Explanation by @Hamish:

The problem is that an implicit conversion is required for a (String, Int) to match up with a (arg1: String, arg2: Int) – by performing the implicit coercion, the compiler ends up with a temporary rvalue which cannot then be passed inout. That's why the error (somewhat confusingly) talks about an immutable value.

Upvotes: 3

Jogendar Choudhary
Jogendar Choudhary

Reputation: 3494

You have two options to do that

First: DO like that

var account3 = (name: "state bank personal", balance: 1000.00)

Or Second: Change method and use like that

func desposit0(amount:Double,account:inout (String,Double))->(String,Double)

use: account.0 and account.1

Upvotes: 0

Related Questions