# Neatly decoding the absence of data

**URL:** <https://forums.swift.org/t/neatly-decoding-the-absence-of-data/30890>\
**Category:** Using Swift\
**Tags:** codable\
**Created:** [November 19, 2019, 12:55am UTC](https://forums.swift.org/t/neatly-decoding-the-absence-of-data/30890 "2019-11-19T00:55:52Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![GalCohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/galcohen/32/8369_2.png) [@GalCohen](https://forums.swift.org/u/GalCohen)\
**Post date:** [November 19, 2019, 12:55am UTC](https://forums.swift.org/t/neatly-decoding-the-absence-of-data/30890/1 "2019-11-19T00:55:52Z")

</div>

I have a function that looks like this (simplified for the sake of the post):

```swift
func request<Response: Decodable>(url: URL, callback: (Result<Response, Error>) -> Void) {
    ...
}

```

This worked fine up until the point where I ran into an API that returned only status codes, and no response data.

How can I decode receiving no data successfully?

I tried `Void` as the generic parameter:

```swift
request(url: URL(string: "endpointWithNoResponse.com")!) { Void in // is that right? typing this on the go...

}

```

That didn't work, because Void isn't Decodable.

I tried

```swift
struct Empty: Decodable {}
request(url: URL(string: "endpointWithNoResponse.com")!) { (empty: Empty) in

}

```

but only really works if the data looks like this `{}`

So what's a nice, Swifty way to address this? Haven't tried `Empty` to handle nothing by overriding the init(decoder)... not sure if that's possible yet.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [November 19, 2019, 1:25am UTC](https://forums.swift.org/t/neatly-decoding-the-absence-of-data/30890/2 "2019-11-19T01:25:14Z")

</div>

Personally, I'd add an overload of the `request` function that omits the generic parameter and associated `Decodable` constraint:

```swift
func request(url: URL, callback: (Result<Void, Error>) -> Void))

```

(alternatively, drop `Result` and pass an `Error?` to the callback)

The downside to this is that it can lead to ambiguities during type inference/overload resolution, but since you're not using the generic parameter elsewhere in your signature, I'm guessing you have to manually write the types in the callback anyway. e.g:

```swift
request(url: URL(string: "blahblah")!) { (response: Result<String, Error>) in
  // ...
}

```

So if that's true, it would fit right in to your existing model and there shouldn't be any ambiguity.

---

<div class="post-metadata">

**Author:** ![GetSwifty](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/getswifty/32/39108_2.png) [@GetSwifty](https://forums.swift.org/u/GetSwifty)\
**Post date:** [November 19, 2019, 6:37pm UTC](https://forums.swift.org/t/neatly-decoding-the-absence-of-data/30890/3 "2019-11-19T18:37:03Z")

</div>

Another option is to have a version where nil is a valid result: `Result<Response?, Error>`, then `callBack(.success(nil))`.

This would likely still require overloading your `request` function, but can be made pretty generic with a `where`.

```swift
func request<Response: Decodable>(url: URL, callback: (Result<Response, Error>) -> Void) 
    where Response: ExpressibleByNilLiteral {
    ...
}

```

---

<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:** [November 19, 2019, 6:54pm UTC](https://forums.swift.org/t/neatly-decoding-the-absence-of-data/30890/4 "2019-11-19T18:54:42Z")

</div>

Alamofire includes an `Empty` type for exactly this purpose, as well as an `EmptyResponse` protocol, so that types can define their own empty value. In our included response serializers we check to see if an empty response is allowed, and, if so, attempt to cast the relevant empty type. It's not super elegant, but it lets us represent it in the type system.

---

<div class="post-metadata">

**Author:** ![Karl](https://avatars.discourse-cdn.com/v4/letter/k/34f0e0/32.png) [@Karl](https://forums.swift.org/u/Karl)\
**Post date:** [November 19, 2019, 7:13pm UTC](https://forums.swift.org/t/neatly-decoding-the-absence-of-data/30890/5 "2019-11-19T19:13:59Z")

</div>

> [@Karl](#):
>
> (alternatively, drop `Result` and pass an `Error?` to the callback)

Let me walk this back, actually. I think Result’s “success” case with a Void payload is much clearer than saying “a request whose callback is given a nil error implicitly succeeded”. Clearer APIs mean fewer mistakes.

---

<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:** [November 19, 2019, 7:28pm UTC](https://forums.swift.org/t/neatly-decoding-the-absence-of-data/30890/6 "2019-11-19T19:28:50Z")

</div>

I agree, and tried to use it in Alamofire, but since `Void` doesn't conform to `Decodable`, it's tough to use in that context.

---

<div class="post-metadata">

**Author:** ![GetSwifty](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/getswifty/32/39108_2.png) [@GetSwifty](https://forums.swift.org/u/GetSwifty)\
**Post date:** [November 19, 2019, 8:24pm UTC](https://forums.swift.org/t/neatly-decoding-the-absence-of-data/30890/7 "2019-11-19T20:24:26Z")

</div>

> [@Jon\_Shier](#):
>
> since `Void` doesn't conform to `Decodable`

I wonder if that will be added if/when we get Protocol conformance for Tuples 🤔
