JARMourato / Kodable

A supercharged extension of Codable
MIT License
84 stars 6 forks source link

Update documentation of wrappedValue force unwrap #1

Closed rogerluan closed 3 years ago

rogerluan commented 3 years ago

Hey 👋

I believe it makes sense to inform users of the lib about this caveat, so I moved it from private to the public instance, and updated the copy a bit. Lemme know your thoughts!

rogerluan commented 3 years ago

On a second thought, I think we'd better throw like this @JARMourato :

- return _value as! U
+ if value == nil {
+     preconditionFailure(message: "Explain why this cant be done, add the docs here")
+ } else {
+     return _value as! U
+ }

Thoughts? 😊

JARMourato commented 3 years ago

This is the error you'd get from the compiler if one were to try to make the initializer throw. It is a known limitation of property wrappers, at least until now.

This is the only case where it would crash

struct Roger: Kodable {
    @Coding("rogdog") var name: String
}
let r = Roger()
print(r.name) // Crashes when getting the name property because the underlying `_value` is nil

I think it is okay to leave it like this, at least for the use I give to the lib.

I like your suggestion. However, I'd prefer if you use fatalError instead of preconditionFailure.

rogerluan commented 3 years ago

Seems good to me! The crash seems fine (like you said, given the context of the lib), but giving a better user feedback is always important - and fatalError does make more sense to me as well 👌

codecov-io commented 3 years ago

Codecov Report

Merging #1 (acdee3e) into main (5d5c783) will not change coverage. The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main        #1   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           13        13           
  Lines          369       371    +2     
=========================================
+ Hits           369       371    +2     
Impacted Files Coverage Δ
Sources/Extensions/Optional+Utilities.swift 100.00% <ø> (ø)
Sources/Kodable/KodableTransformable.swift 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 5d5c783...acdee3e. Read the comment docs.