# Swift 5.9: Type of expression is ambiguous without more context

**URL:** <https://forums.swift.org/t/swift-5-9-type-of-expression-is-ambiguous-without-more-context/65635>\
**Category:** Using Swift\
**Tags:** swift5\
**Created:** [June 17, 2023, 5:20am UTC](https://forums.swift.org/t/swift-5-9-type-of-expression-is-ambiguous-without-more-context/65635 "2023-06-17T05:20:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![softshipper](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/softshipper/32/30420_2.png) [@softshipper](https://forums.swift.org/u/softshipper)\
**Post date:** [June 17, 2023, 5:20am UTC](https://forums.swift.org/t/swift-5-9-type-of-expression-is-ambiguous-without-more-context/65635/1 "2023-06-17T05:20:00Z")

</div>

Hi all,

I have the following code which does not compile:

```swift
private func handleOnText(ws: WebSocket) -> Void {
   ws.onText { (_: WebSocket, text: String) in
      guard let payload = text.data(using: .utf8) else {
         ws.send(generateSerializationErrorMessage())
         return
      }
      
      let message: String = switch Result { try decoder.decode(ConsumerMessage.self, from: payload) } {
         case .success(let msg):
            "Success"
         case .failure():
            "Failed"
      }
      
      ws.send(message)
   }
}

```

The compiler complains:

```
Type of expression is ambiguous without more context

```

As you can see, I am using the new switch expression which is provided by version 5.9 and is causing the compiler error.

I will be very appreciate for the help.

Best regards

---

<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:** [June 17, 2023, 5:30am UTC](https://forums.swift.org/t/swift-5-9-type-of-expression-is-ambiguous-without-more-context/65635/2 "2023-06-17T05:30:53Z")

</div>

> [@softshipper](#):
>
> `switch Result {`

I thought you couldn’t use trailing closure syntax in an `if`, `switch`, or `while` like that.

---

<div class="post-metadata">

**Author:** ![softshipper](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/softshipper/32/30420_2.png) [@softshipper](https://forums.swift.org/u/softshipper)\
**Post date:** [June 17, 2023, 9:30am UTC](https://forums.swift.org/t/swift-5-9-type-of-expression-is-ambiguous-without-more-context/65635/3 "2023-06-17T09:30:51Z")

</div>

Thanks. True.

---

<div class="post-metadata">

**Author:** ![idrougge](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/idrougge/32/3885_2.png) [@idrougge](https://forums.swift.org/u/idrougge)\
**Post date:** [June 18, 2023, 7:50pm UTC](https://forums.swift.org/t/swift-5-9-type-of-expression-is-ambiguous-without-more-context/65635/4 "2023-06-18T19:50:05Z")

</div>

The error message doesn't point that out, though.

---

<div class="post-metadata">

**Author:** ![hamishknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hamishknight/32/14226_2.png) [@hamishknight](https://forums.swift.org/u/hamishknight)\
**Post date:** [June 19, 2023, 4:21pm UTC](https://forums.swift.org/t/swift-5-9-type-of-expression-is-ambiguous-without-more-context/65635/5 "2023-06-19T16:21:22Z")

</div>

The use of the trailing closure isn't the main issue here, the compiler can actually parse it (though it'll emit a warning asking you to enclose in parens), it looks like the issue is with:

```swift
case .failure():

```

You need to either write a pattern to match against the error associated value, or write `case .failure:` instead. With the latest 5.9 snapshot you'll get a marginally better error message (in that it will actually point to the problematic bit of code):

```swift
error: type '()' cannot conform to 'Error' [type_cannot_conform]
    case .failure():
          ^

```

But we still ought to be able to improve this, I've filed [Improve enum element pattern matching diagnostics when the arity of arguments mismatch · Issue #66750 · apple/swift · GitHub](https://github.com/apple/swift/issues/66750) to track that.
