# Using string interpolation for items within collections

**URL:** <https://forums.swift.org/t/using-string-interpolation-for-items-within-collections/36166>\
**Category:** Using Swift\
**Created:** [May 5, 2020, 9:15am UTC](https://forums.swift.org/t/using-string-interpolation-for-items-within-collections/36166 "2020-05-05T09:15:15Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![reflex](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@reflex](https://forums.swift.org/u/reflex)\
**Post date:** [May 5, 2020, 9:15am UTC](https://forums.swift.org/t/using-string-interpolation-for-items-within-collections/36166/1 "2020-05-05T09:15:15Z")

</div>

I'm still coming to grips with the new appendInterpolation() feature in Swift 5.

From my experimentation, it appears that my String extensions are producing the appropriate print statements for my types, but aren't being used when I am trying to print a collection of a type (such as an array).

Is there a trick to this, or is it simply something which isn't catered for?

---

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [May 5, 2020, 9:22am UTC](https://forums.swift.org/t/using-string-interpolation-for-items-within-collections/36166/2 "2020-05-05T09:22:37Z")

</div>

It's hard to say what your problem is without seeing any of your code, but maybe you're confused by the fact that printing the object uses `description`, and printing an array uses `debugDescription`?

```swift
struct Foo: CustomStringConvertible, CustomDebugStringConvertible {
    var description: String { "A" }
    var debugDescription: String { "B" }
}

let foo = Foo()
let fooArray = [Foo()]

print(foo) // A
print(fooArray) // [B]

```

---

<div class="post-metadata">

**Author:** ![reflex](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@reflex](https://forums.swift.org/u/reflex)\
**Post date:** [May 5, 2020, 9:30am UTC](https://forums.swift.org/t/using-string-interpolation-for-items-within-collections/36166/3 "2020-05-05T09:30:28Z")

</div>

I did consider throwing some code in there, but as it was real vanilla stuff I didn't imagine it added anything. But here is what I used as a test.

```
struct A
{
	let x = 5
}

let a = A()
print(a)
print("\(a)")

let b = [a]
print(b)
print("\(b)")

extension String.StringInterpolation
{
	mutating func appendInterpolation(_ c: A)
	{
		appendLiteral("Value is ")
		appendInterpolation(c.x)
	}
}

```

The output I get is…  
**A(x: 5)**  
**Value is 5**  
**[\_\_lldb\_expr\_16.A(x: 5)]**  
**[\_\_lldb\_expr\_16.A(x: 5)]**

So as you can see, while the struct prints fine in line #2, it doesn't when in an array. I find that it will work if I create an interpolation format for an array as well, but that just seems rather unnecessary.

---

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [May 5, 2020, 9:43am UTC](https://forums.swift.org/t/using-string-interpolation-for-items-within-collections/36166/4 "2020-05-05T09:43:26Z")

</div>

> [@reflex](#):
>
> I find that it will work if I create an interpolation format for an array as well, but that just seems rather unnecessary.

`Array<A>` is something different from `A`, so if you want to have customized interpolation for both, you have to implement it for both.

---

<div class="post-metadata">

**Author:** ![reflex](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@reflex](https://forums.swift.org/u/reflex)\
**Post date:** [May 5, 2020, 9:48am UTC](https://forums.swift.org/t/using-string-interpolation-for-items-within-collections/36166/5 "2020-05-05T09:48:34Z")

</div>

> [@cukr](#):
>
> `Array<A>` is something different from `A` , so if you want to have customized interpolation for both, you have to implement it for both

Granted, it is. However one would imagine that printing [A] would automatically have the array calling the A interpolation for each element. After all, that's what it does with CustomStringConvertable.

In any case, I'm guessing the answer to my question is that there isn't any automatic way of doing it?

---

<div class="post-metadata">

**Author:** ![cukr](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/cukr/32/889_2.png) [@cukr](https://forums.swift.org/u/cukr)\
**Post date:** [May 5, 2020, 10:15am UTC](https://forums.swift.org/t/using-string-interpolation-for-items-within-collections/36166/6 "2020-05-05T10:15:30Z")

</div>

> [@reflex](#):
>
> there isn't any automatic way of doing it?

As far as I know there isn't any

---

<div class="post-metadata">

**Author:** ![bzamayo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bzamayo/32/764_2.png) [@bzamayo](https://forums.swift.org/u/bzamayo)\
**Post date:** [May 5, 2020, 10:29am UTC](https://forums.swift.org/t/using-string-interpolation-for-items-within-collections/36166/7 "2020-05-05T10:29:39Z")

</div>

> [@reflex](#):
>
> Granted, it is. However one would imagine that printing [A] would automatically have the array calling the A interpolation for each element. After all, that's what it does with CustomStringConvertable.

The cause is Swift standard library collections are not simply interpolating their elements, they use `debugDescription`. EG the implementation of `Array`'s `description` (for `CustomStringConvertible` conformance) will look something like

```swift
self.map { $0.debugDescription }.joined(",") // obv not real

```

In general, relying on string interpolation overloads to affect printing logic of types you don't control is probably not ideal. You should implement `CustomStringConvertible` and `CustomDebugStringConvertible` directly on `A`.

---

<div class="post-metadata">

**Author:** ![reflex](https://avatars.discourse-cdn.com/v4/letter/r/f05b48/32.png) [@reflex](https://forums.swift.org/u/reflex)\
**Post date:** [May 5, 2020, 10:34am UTC](https://forums.swift.org/t/using-string-interpolation-for-items-within-collections/36166/8 "2020-05-05T10:34:35Z")

</div>

Ok. Thank you both for your inputs. Obviously not the answer I was hoping for, but at least I know it isn't something I'm overlooking.

I've gone back to using CustomStringConvertable, but was interested in trying out the interpolation given it felt like the more modern approach.

---

<div class="post-metadata">

**Author:** ![bzamayo](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bzamayo/32/764_2.png) [@bzamayo](https://forums.swift.org/u/bzamayo)\
**Post date:** [May 5, 2020, 12:43pm UTC](https://forums.swift.org/t/using-string-interpolation-for-items-within-collections/36166/9 "2020-05-05T12:43:38Z")

</div>

Seeing `StringInterpolation` as the 'new' alternative isn't quite right. It's complementary and better for some circumstances, mostly when you are dealing with a type that you control (ie `SQLExpression.StringInterpolation`) rather than a standard library thing, or if you are trying to add custom interpolations with additional parameters and such. But baseline printing behaviour is the purpose of `Custom[Debug]StringConvertible`. Hope that helps.
