# \[Draft\] Throwing Properties and Subscripts

**URL:** <https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786>\
**Category:** Discussion\
**Created:** [March 14, 2016, 3:07am UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786 "2016-03-14T03:07:09Z")\
**Posts on this page:** 15\
**Page:** 2

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [December 23, 2019, 12:02pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/21 "2019-12-23T12:02:34Z")

</div>

Has there been any development regarding this topic?

---

<div class="post-metadata">

**Author:** ![AnuzaMaxima](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anuzamaxima/32/12728_2.png) [@AnuzaMaxima](https://forums.swift.org/u/AnuzaMaxima)\
**Post date:** [December 23, 2019, 12:32pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/22 "2019-12-23T12:32:09Z")

</div>

How use of Optional type or Result is not sufficient enough ?

---

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [December 23, 2019, 12:45pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/23 "2019-12-23T12:45:12Z")

</div>

> [@AnuzaMaxima](#):
>
> How use of Optional type or Result is not sufficient enough ?

The same way as using `Optional` or `Result` everywhere instead of `throws` is not sufficient enough: The former loses the information about the reason **_why_** the desired outcome wasn't achieved and the latter is **A:** cumbersome to deal with and **B:** in case of a settable property allows one to set the value to an error, which is nonsensical in cases where the value itself is never meant to contain an error.

---

<div class="post-metadata">

**Author:** ![AnuzaMaxima](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/anuzamaxima/32/12728_2.png) [@AnuzaMaxima](https://forums.swift.org/u/AnuzaMaxima)\
**Post date:** [December 23, 2019, 4:34pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/24 "2019-12-23T16:34:46Z")

</div>

The code at the caller site would still had to handle failure , either it throwing or optional result. I doubt that desired behaviour can be transparent.

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [December 23, 2019, 4:36pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/25 "2019-12-23T16:36:46Z")

</div>

I think we still have a semantic problem of throwing setter used in tandem with throwing mutator (it even lurks into [Modify Accessors](https://forums.swift.org/t/pitch-modify-accessors/31872)).

---

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [December 23, 2019, 4:41pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/26 "2019-12-23T16:41:13Z")

</div>

> [@AnuzaMaxima](#):
>
> The code at the caller site still needs to handle failure , either it throwing or optional result. I doubt that desired behaviour can be transparent.

I don’t see how you find this:

```swift
func doSomethingDangerous() throws {
    try callThrowingMethod()
}

```

Behaviorally different from this:

```swift
func doSomethingDangerous() throws {
    try accessThrowingProperty
}

```

---

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [December 23, 2019, 4:46pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/27 "2019-12-23T16:46:37Z")

</div>

> [@Lantua](#):
>
> I think we still have a semantic problem of throwing setter used in tandem with throwing mutator (it even lurks into [Pitch: Modify Accessors](https://forums.swift.org/t/pitch-modify-accessors/31872)).

Do you mean the `defer` pitfall? In my opinion, this is a general problem with coroutines that has less to do with property accessors and more to do with the way coroutines are (or, more precisely, aren’t) supposed to interact with errors. If we had throwing `modify` accessor, its apparent `throws`-ness would simply no longer be dependent upon the `yield`ed block, no? Or, maybe I misunderstood you. Could you, please, elaborate?

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [December 23, 2019, 4:49pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/28 "2019-12-23T16:49:54Z")

</div>

Even with `get-set`

```swift
var foo: Foo {
  get { ... }
  set throws { ... }
}

try foo.throwSomething()

```

as is, even if `throwSomething` throws, the `set` is invoked.  
If `set` also throws, we're now left with 2 distinct exceptions, which is quite a problem.

We may say, if `throwSomething` throws, then `set` is not called, but that is vastly different from other accessors (especially with stored variable). We may add new `DoubleException: Error` but it gets clunky really easily.

There's also a question if `set` should be called in the first place, but to say no would be source breaking. So one should weigh that option rather carefully.

---

<div class="post-metadata">

**Author:** ![yuriferretti](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/yuriferretti/32/4452_2.png) [@yuriferretti](https://forums.swift.org/u/yuriferretti)\
**Post date:** [December 23, 2019, 4:59pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/29 "2019-12-23T16:59:07Z")

</div>

This [Draft PR](https://github.com/apple/swift/pull/22749) from @suyashsrijan is on the right track!!

---

<div class="post-metadata">

**Author:** ![suyashsrijan](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/suyashsrijan/32/5292_2.png) [@suyashsrijan](https://forums.swift.org/u/suyashsrijan)\
**Post date:** [December 23, 2019, 5:02pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/30 "2019-12-23T17:02:47Z")

</div>

There’s still more work to do :-) I think the two remaining things to do is restore LValueAccessKind and support \_read/\_modify.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [December 23, 2019, 5:09pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/31 "2019-12-23T17:09:59Z")

</div>

I might miss something from the modify thread but why is it a problem?

Assume we had typed throws and both `set` and the method would throw different error types such as `A` and `B`. Then either we will get a variadic generic `Unit` type which itself conforms to `Error` when all its generic type parameters also conform to `Error`, or we fallback to the plain `Error` instead if there is no `Unit` yet.

```swift
do {
  try foo.mutateAndThrow()
} catch is Unit<A, B> {
  ...
}

do {
  try foo.mutateAndThrow()
} catch is Error {
  switch error {
  case let e as A:
    ...
  case let e as B:
    ...
  default:
    ...
  }
}

```

With the mentioned features we can let the compiler infer the error type automatically and box it if necessary. For now it should be fine to fallback to `Error` no?

---

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [December 23, 2019, 5:29pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/32 "2019-12-23T17:29:55Z")

</div>

> [@Lantua](#):
>
> There's also a question if `set` should be called in the first place, but to say no would be source breaking. So one should weigh that option rather carefully.

Wait, why would it be source breaking when the behavior is only present in throwing setters, which are purely additive?

Given a non-throwing settable property, what happens if a mutating method throws? Is the setter called? If so, what is the `newValue` equal to?

---

<div class="post-metadata">

**Author:** ![Lantua](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/lantua/32/8868_2.png) [@Lantua](https://forums.swift.org/u/Lantua)\
**Post date:** [December 23, 2019, 5:59pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/33 "2019-12-23T17:59:54Z")

</div>

> [@DevAndArtist](#):
>
> I might miss something from the modify thread but why is it a problem?

Nothing is wrong really. Though I'd feel weird if the `Unit` is introduced solely for this usage.

And if we're to change the `set` semantic we might as well do that. If I interpret @John_McCall's post [here](https://forums.swift.org/t/pitch-modify-accessors/31872/57) correctly, that may not be off the table. Though after re-reading it, I could be totally off the mark there 🤔.

> [@technogen](#):
>
> Wait, why would it be source breaking when the behavior is only present in throwing setters, which are purely additive?

I mean that we can either introduce `set throws` with different semantic and be inconsistent with `set`, or retroactively change `set` semantic which is source breaking (or do something else).

> [@technogen](#):
>
> Given a non-throwing settable property, what happens if a mutating method throws? Is the setter called? If so, what is the `newValue` equal to?

The setter is called with the value being whatever it is right before the throw happens.

---

<div class="post-metadata">

**Author:** ![DevAndArtist](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/devandartist/32/287_2.png) [@DevAndArtist](https://forums.swift.org/u/DevAndArtist)\
**Post date:** [December 23, 2019, 6:04pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/34 "2019-12-23T18:04:21Z")

</div>

> [@Lantua](#):
>
> Though I'd feel weird if the `Unit` is introduced solely for this usage.

Same, therefore I hope if we eventually get that type (not clear yet), it will solve more problems than this one.

---

<div class="post-metadata">

**Author:** ![technogen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/technogen/32/45489_2.png) [@technogen](https://forums.swift.org/u/technogen)\
**Post date:** [December 23, 2019, 6:17pm UTC](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786/35 "2019-12-23T18:17:31Z")

</div>

> [@Lantua](#):
>
> The setter is called with the value being whatever it is right before the throw happens.

Now I see where the dual-error problem comes from. The compounding error problem exists outside this use case, though. I've found myself repeatedly hitting this design problem:

```swift
init(from decoder: Decoder) throws {
    let container = decoder.singleValueContainer()
    if let firstTry = try? container.decode(First.self) {
        // ...
    } else if let secondTry = try? container.decode(Second.self) {
        // ...
    } else {
        // What exactly do I throw here?
    }
}

```

On one hand, I would want to throw an aggregate error that contains the errors from each attempt, so that diagnosing the issue becomes easier. On the other hand, I'd be exposing a whole lot of implementation details by doing that and my gut tells me to define my own `Error` type and throw that.

Regardless of what I'd choose, the key reason why this situation is not a fundamental problem is that I do indeed have a _choice_ and the compiler forces me to choose.

What if the compiler would also force me to choose in this exact manner in case I declared a throwing setter? The first thing that comes to mind is promoting the `newValue` to a `Result` if the setter `throws`:

```swift
struct A {
    enum Error: Swift.Error {
        case somethingWentWrong
    }

    var name: String

    mutating func setNameAndThrow(to name: String) throws {
        self.name = name
        throw Error.somethingWentWrong
    }
}

struct B {
    var a: A {
        get { A(name: "John Appleseed") }

        set throws { 
            // `newValue` has the type `Result<A, Error>`
            switch newValue {
                case .success(let a):
                    // Do the actual setting or throw something custom
                case .failure(let error):
                    // Throw the error directly or otherwise make the same choice as above.
            }
        }
}

var b: B()
try b.a.setNameAndThrow(to: "Bob Dylan")

```

Meanwhile, in the parallel universe where function parameters can also be marked as throwing, the `newValue` is a throwing parameter instead of a `Result`.

[Previous page](https://forums.swift.org/t/draft-throwing-properties-and-subscripts/1786.md?page=1)
