Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-7640] assertTypeIsEncodable should throw, not preconditionFailure #50181

Closed
swift-ci opened this issue May 9, 2018 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. Codable Area → standard library: `Codable` and co.

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented May 9, 2018

Previous ID SR-7640
Radar None
Original Reporter omnius (JIRA User)
Type Bug
Status Resolved
Resolution Won't Do
Additional Detail from JIRA
Votes 0
Component/s
Labels Bug, Codable
Assignee None
Priority Medium

md5: f7b9e8bd44203880dbd6c4d0b051578e

Issue Description:

Docs for JSONEncoder.encode() state:

  • The value fails to encode, or contains a nested value that fails to encode—this method throws the corresponding error.

Yet the actual implementation appears to do something more like

https://gist.github.com/mishagray/3ee82a3a82f357bfbf8ff3b3d9eca5cd#file-realmswift-codable-swift-L19

i.e., a preconditionFailure.

Testing the following in a playground

let encodable = [NSError(domain: "domain", code: 100, userInfo: nil)]

try? JSONEncoder().encode(encodable)

we see that, unlike the promised throw, execution halts with a fatal error.

This is contrary to desirable function, in that there is no way to determine whether, for example, an Array (which claims conformance with Encodable and therefore can't be skipped via a guard) will successfully encode or simply crash.

Ideally, this is exactly the purpose of try, which should allow us to get a result or not if none can be had.

@belkadan
Copy link
Contributor

belkadan commented May 9, 2018

In Swift 4.1, Array is now only Encodable if its elements are Encodable. Even without that, though, precondition is the correct behavior. The programmer should know what they are trying to encode, and it is a programmer error if they try to encode something that isn't Encodable. Error and try are only meant for things that are out of the programmer's control, like disk or network access or parsing user input.

@swift-ci
Copy link
Collaborator Author

swift-ci commented May 9, 2018

Comment by Stephen Goodman (JIRA)

Thank you Jordan, that is good to know.

What do you suggest is the ideal pattern is for the case of writing tests against bad input? These can no longer be written to confirm that an array is not encoded (as expected). Does the compile-time check remove the need for such tests?

Thanks again

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. Codable Area → standard library: `Codable` and co.
Projects
None yet
Development

No branches or pull requests

2 participants