21

How different are tuples in swift from structures? (1)

As I understand, both tuples and structures can be sent by value instead by reference in function calls, returns, right?

Also, I know that if have

var A : StructureX
var B : StructureX

I know that structure A and B have the same Type, which is StructureX. But...

let A : (Int, String)
let B : (Int, String)

Are A and B tuples the same Type? (2) What are the advantages about using Tuples instead of structures? (3)

2 Answers 2

24

I find it's easiest to conceptualize Swift Tuples as "Anonymous Structs" with a few critical differences. They behave similarly, but a struct has a formal definition and allows more control over mutability, while tuples allow for pattern matching.

Similarities Between Tuples and Structs

  • Both may have any number of members of any type, including closures
  • Both can be constructed inline (see typealias in the code below)
  • Both prevent mutation of any members if declared as constants
  • If a tuple has labeled members, both structs and tuples allow member access by label

Differences Between Tuples and Structs

  • Structs require a definition before use
  • Structs do not allow pattern matching against their members
  • Structs allow mutability of members declared as variables if the instance is a variable
  • Tuples do not allow mutating functions or functions that refer to any of its members
  • Tuples may not implement Protocols
  • If a tuple has anonymous members, its members can be accessed by index, unlike structs

Some code for a playground illustrating these differences and similarities

// All commented code causes a compilation error. Uncomment to view error messages.

struct StructureX {
    let a: Int = 0
    var b: String = "string"
}

//
// Struct member variability
//
var structureA: StructureX = StructureX()
let structureB: StructureX = StructureX()
//structureA.a = 2              // declared as a constant, instance is variable
structureA.b = "allowed"        // declared as a variable, instance is variable
//structureB.a = 2              // declared as constant, instance is constant
//structureB.b = "not allowed"  // declared as constant, instance is constant
structureA = structureB         // these are the same type
structureA


//
// A tuple can't be used as a literal to construct a struct.
//
//let StructureC: StructureX = (a: 17, b: "nope")


//
// Typealias a labeled tuple and it can be constructed similarly to a struct
//
typealias StructureT = (a: Int, b: String)
var structureD: StructureT = StructureT(a: 0, b: "asdf")
structureD
//structureD = structureA       // but they are distinct types



let emptyTuple: () = ()         // philosophically, isn't this the definition of Void?
print(emptyTuple)               // prints as ()
let single: (Int) = (23)
//let namedSingle: (a: Int) = (a: 42)


//
// Tuple Labeled Member Access
//
var labeledTupleA: (a: Int, b: String) = (a: 0, b: "string")
labeledTupleA.0 = 5
labeledTupleA.a
labeledTupleA

var check: (a: Int, b: String)
check = labeledTupleA           // same type
check

//
// Tuples can have functions/closures
//
let labeledTupleB: (Int, String, fun: () -> Void) = (0, "string", { () -> Void in
    print("hi")
})
labeledTupleB.1
labeledTupleB.fun()
//labeledTupleB.0 = 10          // this tuple is a constant, so all of its members are constant


//
// Tuples with members of the same type, but differet labels are not of the same type
//
var labeledTupleC: (c: Int, d: String) = (c: -1, d: "fail")
//labeledTupleC = labeledTupleA
//labeledTupleC = labeledTupleB


//
// Tuples with anonymous members matching the type pattern of a labeled member tuple are of equivalent type
//
var unlabeledTuple: (Int, String) = (0, "good")
unlabeledTuple = labeledTupleA
unlabeledTuple = labeledTupleC


//
// Tuples with closures may not refer to sibling members
//
var labeledTupleD: (de: Int, df: (Int) -> Void) = (de: 0, df: { (num: Int) -> Void in
    //de += num
    //self.de += num
    print(num)
})

labeledTupleD.de
labeledTupleD.df(1)


//
// Tuples allow pattern matching, Structs do not
//
//switch structureA {
//case (let i, let s):
//    print(i, s)
//default:
//    break
//}

switch labeledTupleD {
case (_, let closure):
    closure(123)
default:
    break
}
4
  • This seems pretty good to me. Although I still can't see how good tuples with anonymous members can be (specially indexed member, which seems to be really bad programming), I wasn't aware about the pattern matching. I'm going to take a look at this now!
    – rodrigogq
    Dec 9, 2014 at 19:30
  • It seems like something that will be defined over the course of the next few years as best practices emerge. For now, I view them as useful for returning multiple values easily in a few small places without defining a struct that's only used once. If I find myself using an anonymous tuple in multiple places, I formalize it as a struct.
    – Ralfonso
    Dec 9, 2014 at 19:48
  • 1
    The biggest problems with tuples that I'm having is that you can't define a single value named tuple. Sounds silly, but there are cases when it might be useful, mostly for consistency sake. Jan 11, 2016 at 6:17
  • What does this mean: "Both can be constructed inline"?
    – Julian A.
    Mar 23, 2017 at 4:53
7

I'm not sure the official terminology around tuples however, you declare them as if they were a special kind of type:

let A : (Int, String)

Maybe we could say that A is now a variable of type tuple? However, not all tuples are the same. If you declare variable of type tuple and try to assign it a tuple with parameters that differ in count, sequence, or type you'll get a compiler error. This will fail

let A : (Int, String) = ("Bob", 1234, 4.0) 

Though this works fine:

let A : (Int, String) = (1234, "Bob")

Of course this strong type safety is also enforced with structs.

As far as the advantages, here are some thoughts on the differences I'm aware of.

Structures require that you define them before using them. Tuples, on the other hand, let you return an arbitrary list of values. How is this useful? I have an iPad app with a shopping cart view controller in it. There is a summary view in the cart view that displays a current status of what's in the cart at any given time--sometimes just normal items, but RMA items and items on re-order are also potentially in the cart. I have a method on my shopping cart class that returns a tuple containing cart count, RMA count, re-order count, and total count. I don't have to declare a structure to get back all four values. It's very convenient:

class Cart : NSManagedObject {
    ...
    var totals : (cartCount:Int, rmaCount:Int, reorderedCount:Int, totalCount:Int) {
        let cart = ...    // Calculate cart count
        let rma = ...     // Calculate rma count
        let reorder = ... // Calculate reorder count
        let total = cart + rma + reorder // Add them all up

        return (cart, rma, reorder, total)
    }
}

In my cart view:

let cartValues = cart.totals
self.summaryView.cartCountLabel.text = "\(cartValues.cartCount)"       
self.summaryView.rmaCountLabel.text = "\(cartValues.rmaCount)"    
self.summaryView.reorderCountLabel.text = "\(cartValues.reorderedCount)"    
self.summaryView.totalCountLabel.text = "\(cartValues.totalCount)"

There may be other reasons, but convenience is the most compelling one for me to prefer tuples in this scenario.

1
  • It seems to be very convenient. Although it can also produce some errors if we don't use tuples with named members as long as we keep upgrading the software. If we guarantee everyone is naming it then, it should be ok. I really appreciate your answer!
    – rodrigogq
    Dec 9, 2014 at 19:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.