Single-element labeled tuples?

TIL that single-element labeled tuples are in the language in at least one place: How do you get labeled associated values from Mirror children? - #3 by Jessy

I like them as placeholders for when I know something else is coming, but it's not worth making a struct for because it will only be the return type for one function. And I wish I didn't need this workaround:

public enum Tuple { }

public extension Tuple {
  /// Used to create a 2-tuple, as a single-element tuple can't have a label.
  typealias Placeholder = Void

  /// Stuff an element into a tuple that will have a labeled first element
  /// when converted to a return value.
  static func `init`<LabeledElement>(
    _ labeledElement: LabeledElement
  ) -> (LabeledElement, Placeholder) {
    ( labeledElement, Placeholder() )
  }
}
func makeIllustrations() -> [(image: String, Tuple.Placeholder)] {
  [ ( image: "🐻", Tuple.Placeholder() ),
    Tuple.`init`("🍯"),
    ( image: "🐝", () )
  ]
}

XCTAssertEqual(makeIllustrations()[1].image, "🍯")
1 Like