# Extract Payload for enum cases having associated value

**URL:** <https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606>\
**Category:** Pitches\
**Tags:** enums, associatedvalue\
**Created:** [August 4, 2019, 4:16pm UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606 "2019-08-04T16:16:24Z")\
**Posts on this page:** 20\
**Page:** 11

<div class="post-metadata">

**Author:** ![GLanza](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glanza/32/3827_2.png) [@GLanza](https://forums.swift.org/u/GLanza)\
**Post date:** [August 26, 2019, 9:30pm UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/203 "2019-08-26T21:30:17Z")

</div>

> [@gwendal.roue](#):
>
> **Edit** : Despite my sample code, I don't like those throwing getters at all. Ideally, enums would return optionals, because this is a case of [simple domain error](https://github.com/apple/swift/blob/master/docs/ErrorHandlingRationale.rst#id11), just like `String.toInt() -> Int?` . I'm fond of the [Error Handling Rationale and Proposal](https://github.com/apple/swift/blob/master/docs/ErrorHandlingRationale.rst#simple-domain-errors), it is my pillow book.

I didn't read the gist yet, but I will tomorrow. Just wanted to give my 2 cents about the nil, instead of throwing. In cases where the associated value is optional we would loose the information of "not the right case" vs "right case, empty associated value".

Note how the `get()` function of `Result` throws. This would be in accordance with that choice.

---

<div class="post-metadata">

**Author:** ![Jumhyn](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jumhyn/32/3891_2.png) [@Jumhyn](https://forums.swift.org/u/Jumhyn)\
**Post date:** [August 26, 2019, 9:38pm UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/204 "2019-08-26T21:38:40Z")

</div>

> [@GLanza](#):
>
> In cases where the associated value is optional we would loose the information of "not the right case" vs "right case, empty associated value".

There's no information lost, you'd just be distinguishing between `nil` and `.some(nil)` which is perfectly valid today. The ergonomics would be perfectly reasonable:

```swift
enum Foo {
    case bar(Int?)
    case baz
}

let x = Foo.bar(nil)
let y = Foo.baz

if let bar = x[keyPath: \.bar] {
    print("It was bar")
    if let value = bar {
        print("...and had an associated value")
    } else {
        print("...and had no associated value")
    }
} else {
    print("It was not bar")
}

```

---

<div class="post-metadata">

**Author:** ![GLanza](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glanza/32/3827_2.png) [@GLanza](https://forums.swift.org/u/GLanza)\
**Post date:** [August 26, 2019, 9:41pm UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/205 "2019-08-26T21:41:33Z")

</div>

I understand that, but recently we flattened `??` for `try?`. I believe that if possible it would be nice to not have optionals of optionals.

---

<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:** [August 26, 2019, 11:18pm UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/206 "2019-08-26T23:18:20Z")

</div>

`Result.get()` throws because `Result`'s intended use case is for suspending and resuming possibly-throwing operations, and `get` is the "resume" operator. I don't think it's an appropriate analogy.

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [August 27, 2019, 12:54am UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/207 "2019-08-27T00:54:52Z")

</div>

> [@Joe\_Groff](#):
>
> While you can read out of either as a `T?` value, you'd only be able to write back to the former as a `T` , and the write operation may go to a black hole.

Thanks for the post, Joe! It'd definitely be nice to design for something that folds optional chaining into this. I'm not sure I understand this part of your post, though. Are you suggesting that `OptionalWritableKeyPath` sets go into the black hole? Or that current semantics do?

> [@GLanza](#):
>
> I [proposed](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/183) a throwing subscript instead.

I had a similar thought but hadn't had time to chime in till now. Other languages model prism setter operations by returning an `Either`/`Result` where one case represents a successful set operation and the other represents failure, so a throwing subscript makes sense to me as the "Swifty" way to capture this.

> [@GLanza](#):
>
> As opt in, yes. Generated properties are very invasive. I believe that users should be able to choose.

I'd like to refresh this conversation around ergonomics and the pushback against "generated properties" by bringing up an alternative that I thought I mentioned earlier on:

Swift could introduce a brand new trailing `?` syntax for unwrapping enum associated values. I've chatted with core team members about this before and it'd be a nice, non-breaking, non-ambiguous syntax that would allow for computed properties to live in harmony on enums with cases of the same name:

```swift
result.success? // Value?

```

---

<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:** [August 27, 2019, 1:19am UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/208 "2019-08-27T01:19:34Z")

</div>

> [@stephencelis](#):
>
> Swift could introduce a brand new trailing `?` syntax for unwrapping enum associated values. I've chatted with core team members about this before and it'd be a nice, non-breaking, non-ambiguous syntax that would allow for computed properties to live in harmony on enums with cases of the same name:
> 
> ```swift
> result.success? // Value?
> 
> ```

I'm not sure that's unambiguous, since you would still be able to compose further postfix operations to the end of the expression.

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [August 27, 2019, 1:23am UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/209 "2019-08-27T01:23:57Z")

</div>

Ah of course. I do wonder if there's a path forward in unifying optional chaining, optional properties, and enum prisms, even if some source breakage needs to happen.

But maybe the default (for now) is to parenthesize before the trailing `?`. If you explicitly parenthesize afterwards you can disambiguate.

```swift
enum Foo {
  case bar(String)
  var bar: String? { nil }
}

Foo.bar("hello").bar?.count // nil
// same as
(Foo.bar("hello").bar)?.count // nil
// vs.
(Foo.bar("hello").bar?)?.count // some(5)

```

---

<div class="post-metadata">

**Author:** ![anandabits](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anandabits/32/355_2.png) [@anandabits](https://forums.swift.org/u/anandabits)\
**Post date:** [August 27, 2019, 1:29am UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/210 "2019-08-27T01:29:25Z")

</div>

> [@Joe\_Groff](#):
>
> the write operation may go to a black hole

Are you suggesting the write would go into a black hole if the current value has a different case? Just dropping writes in this case seems like a design that would lead to bugs. Why shouldn’t the write throw when the value has the wrong case? As I mentioned upthread, injection could be offered as an additional / alternative operation to the setter that does not need to throw (or fail silently) when the case is the last component of the path.

---

<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:** [August 27, 2019, 1:45am UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/211 "2019-08-27T01:45:39Z")

</div>

There are benefits to throwing, sure. That would also allow the successful path to be a symmetric operation in reads and writes, which fits better into the existing storage model. Enum key paths you can write through are sort of jumping the gun on two points, though: we don't have throwing lvalue accesses yet, and we don't have mutation for enums at all either. The problem of "prisms" already comes up in the existing stable of key path components with optional chaining. I think a reasonable path forward that wouldn't require source breakage might be:

- Introduce enum key path components as read-only components that produce `Payload?` values
- In a future Swift that supports all the necessary prerequisites, we can introduce `MaybeWritableKeyPath<T, U>` as a subclass of `KeyPath<T, U?>`, and transparently upgrade optional chaining and enum payload components to this subclass. This would allow the old read-only key path interface to work by producing optionals, and we can introduce a new throwing key path accessor that gives writable access to the storage but throws if it doesn't exist.

---

<div class="post-metadata">

**Author:** ![stephencelis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/stephencelis/32/2675_2.png) [@stephencelis](https://forums.swift.org/u/stephencelis)\
**Post date:** [August 27, 2019, 1:50am UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/212 "2019-08-27T01:50:59Z")

</div>

> [@Joe\_Groff](#):
>
> I think a reasonable path forward that wouldn't require source breakage might be:

I like the idea but I'm not sure I understand how it's less source-breakage. Wouldn't an existing enum with a case and a property of the same name still break without syntax to disambiguate?

---

<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:** [August 27, 2019, 2:12am UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/213 "2019-08-27T02:12:00Z")

</div>

Adding new declaration names can break source in various annoying ways, but we generally consider that to be acceptable.

---

<div class="post-metadata">

**Author:** ![GLanza](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glanza/32/3827_2.png) [@GLanza](https://forums.swift.org/u/GLanza)\
**Post date:** [August 27, 2019, 9:46am UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/214 "2019-08-27T09:46:00Z")

</div>

> [@Joe\_Groff](#):
>
> `Result.get()` throws because `Result` 's intended use case is for suspending and resuming possibly-throwing operations, and `get` is the "resume" operator. I don't think it's an appropriate analogy

Thank you. 🙂

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [August 27, 2019, 11:29am UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/215 "2019-08-27T11:29:55Z")

</div>

> [@GLanza](#):
>
> I believe that if possible it would be nice to not have optionals of optionals.

Nested optionals can obviously be used and misused just as nested arrays can. But they just as obviously have utility, a clear and unambiguous syntax, and often map well to well-defined semantics.

Removing nested optionals should be a clear anti-goal, since it would reduce expressivity and make it harder to solve problems.

That said, they could probably need better ergonomics, especially when you’re dealing with combinations of `Any?` and casting, and you only care about the innermost value.

---

<div class="post-metadata">

**Author:** ![GLanza](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glanza/32/3827_2.png) [@GLanza](https://forums.swift.org/u/GLanza)\
**Post date:** [August 27, 2019, 1:25pm UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/216 "2019-08-27T13:25:27Z")

</div>

> [@sveinhal](#):
>
> Nested optionals can obviously be used and misused just as nested arrays can. But they just as obviously have utility, a clear and unambiguous syntax, and often map well to well-defined semantics.

I have no doubt about it, never said the contrary.

> [@sveinhal](#):
>
> Removing nested optionals should be a clear anti-goal, since it would reduce expressivity and make it harder to solve problems.

Never asked to remove nested optionals from the language. Just, if possible that I would prefer not to see them in this context.

Last thing I would like to see is

```swift
enum Foo { case bar(String?) }

let val = e[case: \.bar] // String??

if let optionalAssociatedValue = e[case: \.bar], // String?
    let associatedValue = optionalAssociatedValue { ... } // String

// vs

let val = try? e[case: \.bar] // String? 
if let associatedValue = try? e[case: \.bar] { ... } // String

```

The difference between the two is that if for my algorithm "not bar" and "is bar but nil" is the same thing I can still do `try?` and get an unwrapped associated value from the if let.

Writing `try?` is an explicit and significative decision that the developer can take to say "I don't care about the error, drop it". With double optional you can't ignore that this operation can fail.

This is just my opinion.

---

<div class="post-metadata">

**Author:** ![sveinhal](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sveinhal/32/3992_2.png) [@sveinhal](https://forums.swift.org/u/sveinhal)\
**Post date:** [August 27, 2019, 1:44pm UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/217 "2019-08-27T13:44:48Z")

</div>

You can also do

```swift
if case let associatedValue?? = e[case: \.bar] {
  // ...
}

```

There’s lots of ways to deal with both optionals and throwing expressions. I don’t have very strong opinions, but at least there is precedent for using optional subscript getters in dictionaries, and there is language support for pattern matching nested enums.

---

<div class="post-metadata">

**Author:** ![GLanza](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glanza/32/3827_2.png) [@GLanza](https://forums.swift.org/u/GLanza)\
**Post date:** [August 27, 2019, 1:47pm UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/218 "2019-08-27T13:47:58Z")

</div>

I don't have a strong opinion either. but I do have a preference. If it comes to vote, I would go for throwable getter and setter, rather than an asymmetric alternative.

---

<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:** [August 27, 2019, 4:40pm UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/219 "2019-08-27T16:40:55Z")

</div>

If we are concerned about collisions with instance properties on the enum, how about a syntax something like `foo.case blah`, or `foo.case blah(blah:blah:)` to disambiguate payload labels, to access a payload optionally? (And by analogy, `\Enum.case blah(blah:blah:)` to get the key path for it.)

---

<div class="post-metadata">

**Author:** ![GLanza](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glanza/32/3827_2.png) [@GLanza](https://forums.swift.org/u/GLanza)\
**Post date:** [August 27, 2019, 4:45pm UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/220 "2019-08-27T16:45:58Z")

</div>

I like it. But it's not a `KeyPath`, that would be another object, right? A subclass?

---

<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:** [August 27, 2019, 4:55pm UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/221 "2019-08-27T16:55:41Z")

</div>

As I mentioned above, it could be a `KeyPath` today, and we could introduce a subclass for conditionally-accessible writable keypaths in the future. Since key path objects are instantiated by the runtime, the future runtime could transparently instantiate eligible key paths to be the new subclass in that future version of Swift.

---

<div class="post-metadata">

**Author:** ![GLanza](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/glanza/32/3827_2.png) [@GLanza](https://forums.swift.org/u/GLanza)\
**Post date:** [August 27, 2019, 5:18pm UTC](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606/222 "2019-08-27T17:18:25Z")

</div>

> [@Joe\_Groff](#):
>
> As I mentioned above, it could be a `KeyPath` today

I'm sorry if you had to repeat yourself. I believed that the new syntax would change things.

[Previous page](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606.md?page=10)

[Next page](https://forums.swift.org/t/extract-payload-for-enum-cases-having-associated-value/27606.md?page=12)
