Marking KeyedEncodingContainerProtocol as unsupported

I've been trying to implement BinaryEncoder/BinaryDecoder, and decided that keyed container will be unsupported (I could just ignore the key, but that'd be too dangerous for my maintenance). So I've been throwing exception everywhere KeyedDecodingContainer is needed. So far so good.

The problem is that all of Encoder container operations is non-throwing. So I setup an UnsupportedContainer that throws whenever any encode(_:) is called.

struct UnsupportedContainer<Key>: Keyed, Unkeyed, SingleValue {
  ...
}

but it feels like I'm doing a lot of unnecessary work.
Is there a better way to setup a Encoder/Decoder with unsupported containers?

It seems to me that you should fatalError instead of throwing. Programmer using your decoder has no reason to ever catch the exception, so why bother? It would either work 100% of the time or 0% of the time. If programmer gets into the 0% situation, the solution is to rewrite their code, not to catch the exception.

1 Like

If you don't want to fatalError, then return a custom container that always throws. Yes this is a pain point of the API. It has already been mentioned somewhere in the forum, but I don't know if an issue has been created (I didn't, despite the fact that I stumbled upon this as well).

1 Like
Terms of Service

Privacy Policy

Cookie Policy