Home > Articles > Data > SQL Server

  • Print
  • + Share This
Like this article? We recommend

Assigning Tuples

Here are some examples of assigning tuples to variables:

let params1 = (1, "Params1", 2.4)
let params2 : UnlabeledTupleType = (1, "Params1", 2.4)
let params3 : LabeledTupleType = (1, "Params3", 2.4)
let params4 : LabeledTupleType = (a:1, b:"Params4", c:2.4)
let params5 = (a:1, b:"Params5", c:2.4)
// let params6 : UnlabeledTupleType = (a:1, b:"Params6", c:2.4) // error
// let params7 : LabeledTupleType = (x:1, y:"Params7", z:2.4) // error

The first two items (params1, params2) create identical anonymous tuples. They are essentially identical and interchangeable.

The next three (params3, params4, params5) create a similar set of labeled tuples. Swift type inferencing enables the construction of params3 even though it doesn't use labels on the right side of the assignment.

The final two assignments are illegal. You cannot assign labels to a type where they aren't defined, or mismatch labels when they are.

You can play assignment tango with tuples, letting the labels, parameters, and argument types dance around:

let v : (a: Int, b:Int) = (1, 2)
let v = (a:1, b:2)
let v : (a: Int, b:Int) = (a:1, b:2)
let v : (a: Int, b:Int) = (b:2, a:1)
  • + Share This
  • 🔖 Save To Your Account