# How do i actually ban a type from participating in string interpolations?

**URL:** <https://forums.swift.org/t/how-do-i-actually-ban-a-type-from-participating-in-string-interpolations/69633>\
**Category:** Using Swift\
**Created:** [January 25, 2024, 10:02pm UTC](https://forums.swift.org/t/how-do-i-actually-ban-a-type-from-participating-in-string-interpolations/69633 "2024-01-25T22:02:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [January 25, 2024, 10:02pm UTC](https://forums.swift.org/t/how-do-i-actually-ban-a-type-from-participating-in-string-interpolations/69633/1 "2024-01-25T22:02:13Z")

</div>

i have a type, let’s call it `GitCommit`, that once looked like this:

```swift
public 
struct GitCommit
{
    private 
    let sha1:SHA1
}

```

and conformed to `CustomStringConvertible` like this:

```swift
extension GitCommit:CustomStringConvertible
{
    public var description:String { "\(self.sha)" }
}

```

this conformance was used everywhere.

```swift
func computeURL() -> String 
{
    "https://github.com/apple/swift/\(self.commit)"
}

```

recently, i wanted to add another field to the type.

```diff
public 
struct GitCommit
{
- private
+ public 
    let sha1:SHA1
+ public
+ let name:String
}

```

i tried to revoke the `CustomStringConvertible` conformance like this:

```swift
@available(*, unavailable)
extension GitCommit:CustomStringConvertible
{
    public
    var description:String { fatalError() }
}

```

as we should no longer be interpolating `GitCommit`s, rather, we should be specifying one of `name` or `sha1`. but i was dismayed to find that this _does not actually prevent_ `GitCommit` from appearing in string interpolations.

how do you actually **ban** a type from participating in string interpolations?

---

<div class="post-metadata">

**Author:** ![bbrk24](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/bbrk24/32/27370_2.png) [@bbrk24](https://forums.swift.org/u/bbrk24)\
**Post date:** [January 25, 2024, 11:29pm UTC](https://forums.swift.org/t/how-do-i-actually-ban-a-type-from-participating-in-string-interpolations/69633/2 "2024-01-25T23:29:46Z")

</div>

The only way I can think of is to prevent it from being wrapped in `Any` -- since you can always interpolate an `Any` -- i.e. make it `~Copyable`. I presume the far-reaching consequences of doing this are unacceptable.

---

<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 25, 2024, 11:38pm UTC](https://forums.swift.org/t/how-do-i-actually-ban-a-type-from-participating-in-string-interpolations/69633/3 "2024-01-25T23:38:52Z")

</div>

Similarly, you can also pass anything to `String.init(describing:)`, which is what string interpolations default to. I think my own best suggestion is to provide an explicit `appendInterpolation(_:)` for it, which you then mark deprecated (not unavailable, because that would eliminate it from overload selection):

```swift
extension DefaultStringInterpolation {
    @available(*, deprecated, message: "choose between the sha and the name")
    func appendInterpolation(_: GitCommit) {
      // do the old thing for compatibility
    }
}

```

---

<div class="post-metadata">

**Author:** ![Joe\_Groff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joe_groff/32/1929_2.png) [@Joe\_Groff](https://forums.swift.org/u/Joe_Groff)\
**Post date:** [January 26, 2024, 12:09am UTC](https://forums.swift.org/t/how-do-i-actually-ban-a-type-from-participating-in-string-interpolations/69633/4 "2024-01-26T00:09:13Z")

</div>

If you want to have stricter control over how your output is presented, it might be interesting to consider using your own `ExpressibleByStringLiteral` type instead of `String`.

---

<div class="post-metadata">

**Author:** ![taylorswift](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/taylorswift/32/43413_2.png) [@taylorswift](https://forums.swift.org/u/taylorswift)\
**Post date:** [January 26, 2024, 12:26am UTC](https://forums.swift.org/t/how-do-i-actually-ban-a-type-from-participating-in-string-interpolations/69633/5 "2024-01-26T00:26:40Z")

</div>

> [@Joe\_Groff](#):
>
> If you want to have stricter control over how your output is presented, it might be interesting to consider using your own `ExpressibleByStringLiteral` type instead of `String`.

that strikes me as an inconvenient set of places to put this logic, because string conversion is used in a lot of diverse places in our code base. for example:

- interpolating `GitCommit.sha1` into a URL path component
- interpolating `GitCommit.sha1` into a JSON string
- interpolating `GitCommit.sha1` to some human-readable text content of an HTML node
- interpolating `GitCommit.sha1` to some terminal output

all of these sites are using string interpolations, and it doesn’t really make much sense to me, to have to introduce `URL.PathComponent`, `JSON.StringNode`, `HTML.StringNode`, `Terminal.LogText`, etc. just to deal with `GitCommit`.

> [@jrose](#):
>
> I think my own best suggestion is to provide an explicit `appendInterpolation(_:)` for it, which you then mark deprecated (not unavailable, because that would eliminate it from overload selection):

i am actually using `@available(*, deprecated)` on the `CustomStringConvertible` conformance itself, which has the same effect. however, this is a poor workflow generally, for a task that needs to be done quite frequently as `CustomStringConvertible` conformances are revoked:

- `@available(*, deprecated)` only raises warnings the first time the project is built. therefore, you need to do a clean build on each iteration.

- `@available(*, deprecated)` does not mix well today with `.enableExperimentalFeature("StrictConcurrency")`, because strict concurrency produces an enormous volume of unsurpressable warnings (e.g., [`CommandLine.arguments`](https://forums.swift.org/t/commandline-arguments-is-not-concurrency-safe/68095)) that can conceal the deprecation warnings.

- `@available(*, deprecated)` is not really the correct tool for the job, as it still allows the property to be called. it has really become `unavailable`, and that should be enforceable by the compiler.
