What is the state of the art for grabbing a tuple of properties multiple times?

Is there a better solution available these days than a Tuple type?

struct 🇪🇨: Equatable {
  var eq = "🎛"
  var u = 1.0 / 1_000_000
  var al = 13

  let probablyNotEqual = UUID()

  static func == (ecuador0: Self, ecuador1: Self) -> Bool {
    let getProperties = Tuple(\Self.eq, \.u, \.al)
    return getProperties(ecuador0) == getProperties(ecuador1)
  }
}
/// A workaround for not being able to extend tuples.
public struct Tuple<Elements> {
  public var elements: Elements

  public init(_ elements: Elements) {
    self.elements = elements
  }
}

public extension Tuple {
  init<Parameters, Transformed0, Transformed1, Transformed2>(
    _ transform0: @escaping (Parameters) -> Transformed0,
    _ transform1: @escaping (Parameters) -> Transformed1,
    _ transform2: @escaping (Parameters) -> Transformed2
  )
  where Elements == (
    (Parameters) -> Transformed0,
    (Parameters) -> Transformed1,
    (Parameters) -> Transformed2
  ) {
    self.init( (transform0, transform1, transform2) )
  }

  func callAsFunction<Parameters, Transformed0, Transformed1, Transformed2>(
    _ parameters: Parameters
  ) -> (Transformed0, Transformed1, Transformed2)
  where Elements == (
    (Parameters) -> Transformed0,
    (Parameters) -> Transformed1,
    (Parameters) -> Transformed2
  ) {
    ( elements.0(parameters), elements.1(parameters), elements.2(parameters) )
  }
}

I'd say it's very, very close to turn this into struct.

Terms of Service

Privacy Policy

Cookie Policy