# Understanding Codable/Decodable initialization

**URL:** <https://forums.swift.org/t/understanding-codable-decodable-initialization/32528>\
**Category:** Using Swift\
**Tags:** codable\
**Created:** [January 13, 2020, 4:50pm UTC](https://forums.swift.org/t/understanding-codable-decodable-initialization/32528 "2020-01-13T16:50:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![btx1289](https://avatars.discourse-cdn.com/v4/letter/b/b9bd4f/32.png) [@btx1289](https://forums.swift.org/u/btx1289)\
**Post date:** [January 13, 2020, 4:50pm UTC](https://forums.swift.org/t/understanding-codable-decodable-initialization/32528/1 "2020-01-13T16:50:23Z")

</div>

Hello,

I'm trying to get an understanding of how Decodable works. The definition of the protocol is:

```
@_implicitly_synthesizes_nested_requirement("CodingKeys")
public protocol Decodable {
   init(from decoder: Decoder) throws
}

```

The two things I'm not sure I understand are:

1. Where is the default implementation of the init? I see it implemented on a bunch of types, but I haven't been able to find a default implementation. IE, if I create a new type that implements Decodable and don't overwrite the init, what gets called? How does that work?

2. I see that \_implicitly\_synthesizes\_nested\_requirement comes from C code, but I don't see a definition of what it does. (I am trying to understand how default coding keys work as well).

Thanks!

---

<div class="post-metadata">

**Author:** ![owenv](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/owenv/32/6137_2.png) [@owenv](https://forums.swift.org/u/owenv)\
**Post date:** [January 13, 2020, 5:14pm UTC](https://forums.swift.org/t/understanding-codable-decodable-initialization/32528/2 "2020-01-13T17:14:33Z")

</div>

> [@btx1289](#):
>
> Where is the default implementation of the init? I see it implemented on a bunch of types, but I haven't been able to find a default implementation. IE, if I create a new type that implements Decodable and don't overwrite the init, what gets called? How does that work?

Surprisingly, the initializer doesn't have a default implementation, each type that conforms to `Decodable` must provide its own `init(from decoder: Decoder)`. However, it sometimes looks like there's a default implementation because many types don't explicitly write their own. Instead, they're using a "synthesized conformance", which just means that because the compiler has special built-in knowledge of `Decodable`, it can generate the implementation automatically in some cases. Synthesizing the init happens entirely within the compiler implementation, which is why it can't be found in the standard library.

Default `CodingKeys` use a similar mechanism, where the compiler can sometimes insert a synthesized `CodingKeys` type. `@_implicitly_synthesizes_nested_requirement("CodingKeys")` is an attribute that identifies `Decodable` to the compiler as a protocol that is eligible for this synthesis.

It doesn't describe much of the implementation itself, but if you're interested in a high level overview of how `Codable` requirements are synthesized, the original [proposal](https://github.com/apple/swift-evolution/blob/master/proposals/0166-swift-archival-serialization.md) is a helpful resource.

---

<div class="post-metadata">

**Author:** ![Avi](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/avi/32/2962_2.png) [@Avi](https://forums.swift.org/u/Avi)\
**Post date:** [January 13, 2020, 5:15pm UTC](https://forums.swift.org/t/understanding-codable-decodable-initialization/32528/3 "2020-01-13T17:15:17Z")

</div>

Essentially, it's all compiler magic. When it can, the compiler synthesizes `init()` and `CodingKeys` for a type. Only when it fails will the compiler complain that one or the other is missing.

---

<div class="post-metadata">

**Author:** ![itaiferber](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/itaiferber/32/33840_2.png) [@itaiferber](https://forums.swift.org/u/itaiferber)\
**Post date:** [January 13, 2020, 5:24pm UTC](https://forums.swift.org/t/understanding-codable-decodable-initialization/32528/4 "2020-01-13T17:24:07Z")

</div>

To add to the existing answers, for some in-depth information on synthesis, please see [How does automatic Codable compliance work? For the sake of extending it - #3 by itaiferber](https://forums.swift.org/t/how-does-automatic-codable-compliance-work-for-the-sake-of-extending-it/25933/3) (with some additional info in [Inheriting from a Codable class](https://forums.swift.org/t/inheriting-from-a-codable-class/14874)) [among some of my other past posts]. There are also direct links to the implementation that you can skim through, if you're comfortable with C++.

---

<div class="post-metadata">

**Author:** ![btx1289](https://avatars.discourse-cdn.com/v4/letter/b/b9bd4f/32.png) [@btx1289](https://forums.swift.org/u/btx1289)\
**Post date:** [January 14, 2020, 5:11pm UTC](https://forums.swift.org/t/understanding-codable-decodable-initialization/32528/5 "2020-01-14T17:11:49Z")

</div>

Awesome. Thanks so much, this is a big help.
