# DRY in array initialization

**URL:** <https://forums.swift.org/t/dry-in-array-initialization/62396>\
**Category:** Discussion\
**Tags:** init\
**Created:** [January 5, 2023, 12:34am UTC](https://forums.swift.org/t/dry-in-array-initialization/62396 "2023-01-05T00:34:39Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [January 5, 2023, 12:34am UTC](https://forums.swift.org/t/dry-in-array-initialization/62396/1 "2023-01-05T00:34:39Z")

</div>

In the Chart section of the SwiftUI manual, we see a data array made from literals as so:

```swift
var stackedBarData: [ToyShape] = [
    .init(color: "Green", type: "Cube", count: 2),
    .init(color: "Green", type: "Sphere", count: 0),
    .init(color: "Green", type: "Pyramid", count: 1),
    .init(color: "Purple", type: "Cube", count: 1),
    .init(color: "Purple", type: "Sphere", count: 1),
    .init(color: "Purple", type: "Pyramid", count: 1),
    .init(color: "Pink", type: "Cube", count: 1),
    .init(color: "Pink", type: "Sphere", count: 2),
    .init(color: "Pink", type: "Pyramid", count: 0),
    .init(color: "Yellow", type: "Cube", count: 1),
    .init(color: "Yellow", type: "Sphere", count: 1),
    .init(color: "Yellow", type: "Pyramid", count: 2)
]

```

That's a whole of of repetition. Now, when I make structs I often make an initializer which leaves out the labels, so we could get to here:

```swift
var stackedBarData: [ToyShape] = [
// ToyShape(color: String, type: String, count: Int)
   .init("Green", "Cube", 2),
   .init("Green", "Sphere", 0),
   .init("Green", "Pyramid", 1),
...
]

```

My idea is that in the narrow context of an array literal, it'd be nice if we could go all the way to:

```swift
var stackedBarData: [ToyShape] = [
// ToyShape(color: String, type: String, count: Int)
    ("Green", "Cube", 2)
    ("Green", "Sphere", 0)
    ("Green", "Pyramid", 1)
...
]

```

Has anyone played with that idea? I wouldn't be surprised, but I don't recall seeing it. I feel like it fits the Swift idiom of being able to _optionally_ leave out assumable things.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [January 5, 2023, 12:39am UTC](https://forums.swift.org/t/dry-in-array-initialization/62396/2 "2023-01-05T00:39:14Z")

</div>

Clarity over brevity: the information is redundant on _every_ line, but it’s important _once._

It would be nice™ to have a guaranteed optimization for

```swift
var stackedBarData: [ToyShape] = [
    ("Green", "Cube", 2),
    ("Green", "Sphere", 0),
    ("Green", "Pyramid", 1)
].map { .init(color: $0.0, type: $0.1, count: $0.2) }

```

but I suspect that’s pretty involved.

---

<div class="post-metadata">

**Author:** ![Hacksaw](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hacksaw/32/32775_2.png) [@Hacksaw](https://forums.swift.org/u/Hacksaw)\
**Post date:** [January 5, 2023, 12:49am UTC](https://forums.swift.org/t/dry-in-array-initialization/62396/3 "2023-01-05T00:49:22Z")

</div>

Yeah, I definitely like the idea of having to mention those parameters labels at least once, but I'm not sure of the most satisfying way to do it without straining the idiom.

I toyed with:

```swift
var stackedBarData: [ToyShape] = [
    init (color: String, type: String, count: Int) {
    ("Green", "Cube", 2)
   ...
    }
]

```

but rejected it.

I think IDE support might be the best solution here, much as that pains my purist soul.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [January 5, 2023, 1:01am UTC](https://forums.swift.org/t/dry-in-array-initialization/62396/4 "2023-01-05T01:01:02Z")

</div>

You could do it with a result builder, I suppose:

```swift
var stackedBarData: [ToyShape] = array {
  .init(color: $0.0, type: $0.1, count: $0.2)
} from: { // @ArrayBuilder or whatever
  ("Green", "Cube", 2)
  ("Green", "Sphere", 0)
  ("Green", "Pyramid", 1)
}

```

but that would be building the result element by element, and wouldn’t have the total count on hand.

…And of course the new expressions macros could handle it.

---

<div class="post-metadata">

**Author:** ![anon9791410](https://avatars.discourse-cdn.com/v4/letter/a/9fc348/32.png) [@anon9791410](https://forums.swift.org/u/anon9791410)\
**Post date:** [January 5, 2023, 1:25am UTC](https://forums.swift.org/t/dry-in-array-initialization/62396/5 "2023-01-05T01:25:54Z")

</div>

The DRY form is

```swift
var stackedBarData = [
  ("Green", [2, 0, 1]),
  ("Purple", [1, 1, 1]),
  ("Pink", [1, 2, 0]),
  ("Yellow", [1, 1, 2])
]
  .flatMap { color, counts in
    zip(["Cube", "Sphere", "Pyramid"], counts).map {
      ToyShape(color: color, type: $0, count: $1)
    }
  }

```

but Xcode can't indent it properly.
