-1

First, thanks for your Help

for _ in 0..<v {
    let edge = readLine()!.split(separator: " ").map { Int(String($0))! }
    let (a: Int, b: Int, w: Int) = (edge[0], edge[1], edge[2]) 
}

It seems to me that it is simply the process of receiving integers and putting them in the tuple, but a Circular reference error is detected. Can you tell me why?

let edge = readLine()!.split(separator: " ").map { Int(String($0))! }
let (a, b, w) = (edge[0], edge[1], edge[2])

It works well if I remove the type annotations.

  • 2
    According to the answers on https://stackoverflow.com/questions/65977604/what-does-tuple-assignment-var-xp-yq-in-swift it seems that this syntax is trying to declare multiple variables named `Int` and a tuple with labels a, b and w. – Geoff Hackworth Mar 27 '23 at 09:09
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Mar 29 '23 at 17:56

1 Answers1

1

The error is sort of unclear, but syntax let (a: Int, b: Int, w: Int) is not valid for declaring the types of variables inside the tuple. You are effectively declaring 3 variables named Int with no type. And a and b are just labels you'd be able to refer by continue a or break b, except that would also be problematic, since those labels are inside the tuple.

You could do one of the following instead:

Option 1:

let (a, b, w): (Int, Int, Int) = (edge[0], edge[1], edge[2])
print(a, b, w)

This is not even a tuple - we defined 3 independent variables - a, b, and w, all of type Int

Option 2:

let x: (a: Int, b: Int, w: Int) = (edge[0], edge[1], edge[2])
print(x.a, x.b, x.w)

In this case we define a single variable x, of type tuple with 3 elements, labeled a, b and w. Notice that we refer to them via x

Option 2a: We may as well do this: define our type once

typealias MyType = (a: Int, b: Int, w: Int)

and then use it multiple times:

for _ in 0..<v {
    let edge = readLine()!.split(separator: " ").map { Int(String($0))! }
    let x: MyType = (edge[0], edge[1], edge[2]) 
}
timbre timbre
  • 12,648
  • 10
  • 46
  • 77
  • You also don't need explicit typing. (`let x: (a: _, b: _, w: _) = (edge[0], edge[1], edge[2])` or `let x = (a: edge[0], b: edge[1], w: edge[2])`) –  Mar 27 '23 at 20:02
  • Thanks to you, I've grown even more – Kelly Chui Mar 29 '23 at 03:34