Flight-School / AnyCodable

Type-erased wrappers for Encodable, Decodable, and Codable values
https://flight.school/books/codable
MIT License
1.28k stars 132 forks source link

Make AnyEncodable ExpressibleByStringInterpolation #38

Closed pokryfka closed 4 years ago

pokryfka commented 4 years ago

@mattt please let me know if you think it makes sense to update/cleanup this PR and if so, how

I think ExpressibleByStringInterpolation is very useful, I typically add metadata that way

having said that its a one-liner to add in a project, and the challenge seems to be making the tests nice and work properly on different versions/platforms ;-)

mattt commented 4 years ago

@pokryfka First off, thank you for your PR. I appreciate your interest to contribute to this project!

Could you explain your specific use case for this feature? Without a custom string interpolation type (i.e. bare ExpressibleByStringInterpolation), I'm not sure I understand what this gives you that ExpressibleByStringLiteral doesn't.

pokryfka commented 4 years ago

example:

        let durationInSeconds: Float = 1
        var metadata = [String: AnyEncodable]()
        // works
        metadata["duration1"] = AnyEncodable("\(durationInSeconds)s")
        // error: Cannot assign value of type 'String' to subscript of type 'AnyEncodable'
        metadata["duration2"] = "\(durationInSeconds)s"

solved by

extension AnyEncodable: ExpressibleByStringInterpolation {}

this is a very common use case for me, there may be a different solution to that

mattt commented 4 years ago

@pokryfka Oh, huh. I don't think I was fully aware of this behavior, so thanks for spelling that out for me. I'll work on getting the tests to pass reliably on Linux and get this merged soon.

mattt commented 4 years ago

According to NASA's Jet Propulsion Laboratory 15 decimal places of π is sufficient for any physical calculation. Rather than work or explain around the inherently thorny issues of floating-point precision and JSON serialization, let's just use 15 places throughout and call it a day.

mattt commented 4 years ago

Thanks again for your help with this, @pokryfka. Your changes are now live in version 0.3.0.