Codable with references?

You can't solve this in the general case. As you say, it needs a 3-pass initialization process, not the current 2-pass process (although it's not entirely clear how best to break it down into three passes).

The only current solution is to use some kind of optional and set the actual value "later", which typically involves some customization of the decoding process (and possibly preventing the synthesis of Decodable conformance). You can do this in your example, too, by noting that the children array is kinda like an optional when left empty. You can create the First object first with no children, then create all the Second objects with references to their parents, then add all the children to the parent.

The huge drawback (apart from having to refactor code you think of differently) is that the extra step has to involve making something public that probably should be private to First. (Edit: Plus, it tends to expose objects that are in a intermediate/incomplete state, which isn't good either.)

Anyway, this is the point that I gave up (as I said in the original thread) and decided to let @itaiferber go first into this dark valley. :slight_smile: Currently, I think he's too optimistic about the solvability of the problem, but if there's a solution I trust him to find it, or invent it.