# Changing the spelling of @discardableResult or discard

**URL:** <https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178>\
**Category:** Pitches\
**Created:** [July 15, 2024, 7:04pm UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178 "2024-07-15T19:04:31Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![1-877-547-7272](https://avatars.discourse-cdn.com/v4/letter/1/278dde/32.png) [@1-877-547-7272](https://forums.swift.org/u/1-877-547-7272)\
**Post date:** [July 15, 2024, 7:04pm UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/1 "2024-07-15T19:04:31Z")

</div>

The `discard` operator and `@discardableResult` both use the term "discard" in similar, but critically distinct ways. `@discardableResult` uses the term to indicate that a value can be implicitly destroyed, while the `discard` operator uses the term to indicate that a noncopyable value should be destroyed without running the deinitializer.

This inconsistency may be misleading to users who aren't familiar with noncopyable types, and I personally find it annoying. Plus, I think there's still time to change it — while noncopyable types are already part of the language, they haven't gotten significant usage yet due to the fact that they don't support protocols and borrowing switches. And many of the noncopyable types that do exist don't use `discard`.

I think we should consider deprecating the `discard` operator and adding something else. In Rust, this operation [is called `forget`](https://doc.rust-lang.org/std/mem/fn.forget.html), so we could use that name. Or, we could turn `discard` into a function named something like `discardWithoutRunningDeinitializer` (obviously, the exact name can be bikeshedded). The advantage of turning it into a function is that the name can be much more descriptive of what the function does. The `discard` operator doesn't seem to be used as much as most other operators, so a function might be more appropriate.

We could also consider changing the name of `@discardableResult`, but that would likely cause significantly more churn.

What do you all think?

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [July 15, 2024, 8:49pm UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/2 "2024-07-15T20:49:26Z")

</div>

IMHO this would be good alternative for @discardableResult syntax if to change it:

```swift
func foo() -> discardable Int { ... }

```

That would still be quite close to "discard" though.

---

<div class="post-metadata">

**Author:** ![7ombie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/7ombie/32/36317_2.png) [@7ombie](https://forums.swift.org/u/7ombie)\
**Post date:** [July 15, 2024, 11:46pm UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/3 "2024-07-15T23:46:51Z")

</div>

Synonyms include `dispensable`, `expendable` and `droppable`.

---

<div class="post-metadata">

**Author:** ![7ombie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/7ombie/32/36317_2.png) [@7ombie](https://forums.swift.org/u/7ombie)\
**Post date:** [July 15, 2024, 11:50pm UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/4 "2024-07-15T23:50:57Z")

</div>

> [@tera](#):
>
> `func foo() -> discardable Int { ... }`

I do prefer this syntax for `@discardableResult` (just because it's pretty commonplace), and to be honest, I've literally never used the `discard` operator.

---

<div class="post-metadata">

**Author:** ![jeremyabannister](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jeremyabannister/32/10143_2.png) [@jeremyabannister](https://forums.swift.org/u/jeremyabannister)\
**Post date:** [July 15, 2024, 11:51pm UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/5 "2024-07-15T23:51:31Z")

</div>

> [@tera](#):
>
> IMHO this would be good alternative for @discardableResult syntax if to change it:
> 
> ```swift
> func foo() -> discardable Int { ... }
> 
> ```

I’ve been meaning to make a post soon about this type of idea. It seems to me that the discardability of a value is information that should be carried by the value, not by the function that produces it. If I pass a `@discardableResult` function as a closure to another function that will run it and return the value, the resulting value should retain the information that it is discardable. At the moment, I am forced to do this:

```swift
_ = wrapper(generateDiscardableValue)

```

---

<div class="post-metadata">

**Author:** ![Jon\_Shier](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jon_shier/32/921_2.png) [@Jon\_Shier](https://forums.swift.org/u/Jon_Shier)\
**Post date:** [July 16, 2024, 12:00am UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/6 "2024-07-16T00:00:16Z")

</div>

I think `@ignorableResult` is probably the clearest, and implies exactly what it does.

---

<div class="post-metadata">

**Author:** ![7ombie](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/7ombie/32/36317_2.png) [@7ombie](https://forums.swift.org/u/7ombie)\
**Post date:** [July 16, 2024, 12:08am UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/7 "2024-07-16T00:08:08Z")

</div>

This would be nice:

```swift
func foo() -> ignorable Foo { ... }
func foo() -> some ignorable Foo { ... }
func foo() -> any ignorable Foo { ... }

```

Using attributes like `@discardableResult` and `@ignorableResult` gets a bit wordy, considering how often functions have discardable results in some situations (for example, method-chaining APIs).

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [July 16, 2024, 12:14am UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/8 "2024-07-16T00:14:34Z")

</div>

Folks, this thread isn't about redesigning the syntax of `@discardableResult`.

The topic is: does the clash between two uses of "discard" cause confusion, and if so, which of the conflicting uses should be _renamed_ and to what?

---

<div class="post-metadata">

**Author:** ![kavon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/kavon/32/9774_2.png) [@kavon](https://forums.swift.org/u/kavon)\
**Post date:** [July 16, 2024, 12:46am UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/9 "2024-07-16T00:46:59Z")

</div>

> [@1-877-547-7272](#):
>
> I think we should consider deprecating the `discard` operator and adding something else. In Rust, this operation [is called `forget`](https://doc.rust-lang.org/std/mem/fn.forget.html), so we could use that name.

The original SE-390 proposal called it “forget”. For the [second review](https://forums.swift.org/t/second-review-se-0390-noncopyable-structs-and-enums/63866) we renamed it to “discard”. I briefly searched both threads and didn’t spot any bike-shedding for the name. I don’t recall why we changed the name, either.

But, I think “forget” is rather vague as to what will happen to the value. It doesn’t suggest the value is destroyed at all.

I guess `@discardableResult` on a function `f` could be confused as meaning you can write `discard f()` to “discard” the returned value explicitly, but you can’t. In fact, you can’t use `discard` on anything other than `self`.

---

<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:** [July 16, 2024, 1:46am UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/10 "2024-07-16T01:46:03Z")

</div>

> [@xwu](#):
>
> The topic is: does the clash between two uses of "discard" cause confusion

it would be less confusing, if the first page of search results for [swift discard keyword](https://www.google.com/search?q=swift+discard+keyword&sca_esv=6e688277c54b6e6a&sca_upv=1&sxsrf=ADLYWILSgKbAvRCCdCu4pK0L20qwGuDdsg%3A1721094165926&source=hp&ei=FdCVZsSdNoq90PEPna-SoAM&iflsig=AL9hbdgAAAAAZpXeJVrnyurAxwlhxFRb_ebh9rY334YW&ved=0ahUKEwiE_Iezt6qHAxWKHjQIHZ2XBDQQ4dUDCBc&uact=5&oq=swift+discard+keyword&gs_lp=Egdnd3Mtd2l6IhVzd2lmdCBkaXNjYXJkIGtleXdvcmQyBRAhGKABMgUQIRigATIFECEYnwVI7BlQAFj3FnAAeACQAQCYAbEBoAHZEqoBBTEwLjExuAEDyAEA-AEBmAIVoAKzFMICBBAjGCfCAgoQIxiABBgnGIoFwgIREC4YgAQYsQMY0QMYgwEYxwHCAg4QABiABBixAxiDARiKBcICDhAuGIAEGLEDGIMBGIoFwgILEAAYgAQYsQMYgwHCAhcQLhiABBjHARiYBRiZBRieBRiOBRivAcICCBAuGIAEGLEDwgIOEC4YgAQYxwEYjgUYrwHCAggQABiABBixA8ICFBAuGIAEGLEDGIMBGMcBGI4FGK8BwgILEC4YgAQYsQMY5QTCAgUQABiABMICBxAAGIAEGArCAgYQABgWGB7CAggQABgWGB4YD8ICCxAAGIAEGIYDGIoFwgIIEAAYgAQYogSYAwCSBwQzLjE4oAfaxgE&sclient=gws-wiz) didn’t consist entirely of articles about `@discardableResult`, with one reference to `DiscardingTaskGroup` thrown in.

i personally am not confused by it, but i am not the audience to be concerned about. i think unless they go spelunking down TSPL, a new user has a low chance of learning the difference.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [July 16, 2024, 1:49am UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/11 "2024-07-16T01:49:42Z")

</div>

Indeed, a good point.

Reassuringly, though, given @kavon's reminder that we can only `discard self`, the query `swift "discard self"` leads to extremely relevant results, and I think it's not too out the box to think that a user might just google that entire statement if they see only clearly irrelevant results with their first search.

---

<div class="post-metadata">

**Author:** ![1-877-547-7272](https://avatars.discourse-cdn.com/v4/letter/1/278dde/32.png) [@1-877-547-7272](https://forums.swift.org/u/1-877-547-7272)\
**Post date:** [July 16, 2024, 3:18am UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/12 "2024-07-16T03:18:34Z")

</div>

> [@kavon](#):
>
> I guess `@discardableResult` on a function `f` could be confused as meaning you can write `discard f()` to “discard” the returned value explicitly, but you can’t. In fact, you can’t use `discard` on anything other than `self`.

Since noncopyable types are an advanced and rarely-used feature (for now), I think it’s much more likely that a user will know what `@discardableResult` is but won’t know what `discard self` is. In that case, the user may guess that `discard self` is simply sugar for `_ = self`, similar to Rust’s `std::mem::drop`.

---

<div class="post-metadata">

**Author:** ![j-f1](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/j-f1/32/12287_2.png) [@j-f1](https://forums.swift.org/u/j-f1)\
**Post date:** [July 16, 2024, 3:26am UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/13 "2024-07-16T03:26:06Z")

</div>

> [@taylorswift](#):
>
> with one reference to `DiscardingTaskGroup` thrown in

This is another good reason to use a term other than `discard` — `DiscardingTaskGroup` behaves nothing like the `discard self` statement.

---

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [July 17, 2024, 12:58am UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/14 "2024-07-17T00:58:14Z")

</div>

> [@xwu](#):
>
> Folks, this thread isn't about redesigning the syntax of `@discardableResult`.

Sorry to state the obvious but the topic's subject suggests changing `@discardableResult` spelling as one of the options...

---

<div class="post-metadata">

**Author:** ![ellie20](https://avatars.discourse-cdn.com/v4/letter/e/6f9a4e/32.png) [@ellie20](https://forums.swift.org/u/ellie20)\
**Post date:** [July 27, 2024, 6:45am UTC](https://forums.swift.org/t/changing-the-spelling-of-discardableresult-or-discard/73178/15 "2024-07-27T06:45:47Z")

</div>

I think there's a chance that it might be worthwhile at some point to add linear types to the language, which are types whose values, alongside not being implicitly copyable, also aren't implicitly destroyable. The name of a protocol denoting the ability to be implicitly destroyed, such as `Discardable`, should probably be consistent with the spelling of `@discardableResult`. If so, it may become a further rationale for changing the `discard` operator to something else.
