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

Resolve transcode method ambiguity #141

Conversation

bauglir
Copy link
Contributor

@bauglir bauglir commented Apr 14, 2023

The examples in many of the codec packages rely on being able to call transcode providing a Codec type and a string12. #136 removed type annotations from the trailing arguments for transcode(::Type{C}, ...) where {C<:Codec} causing a method ambiguity with transcode(T, src::String) in Julia Base3. This adds an additional method to Base.transcode to resolve this ambiguity.

I have added tests for the Noop codec to ensure the ambiguity is resolved. I'm wondering whether it makes sense to add a test helper function that downstream packages can use to ensure this functionality remains intact. Let me know if that is something I should add.

Fixes #139.

Footnotes

  1. https://github.com/JuliaIO/CodecZlib.jl/tree/f9fddaa28c093c590a7a93358709df2945306bc7#usage

  2. https://github.com/JuliaIO/CodecZstd.jl/tree/6327ffa9a3a12fc46d465dcfc8b30bed91cf284b#usage

  3. https://github.com/JuliaLang/julia/blob/ff7b8eb00bf887f20bf57fb7e53be0070a242c07/base/c.jl#L306

The examples in many of the codec packages rely on being able to call
`transcode` providing a `Codec` type and a string[^1][^2]. JuliaIO#136 removed
type annotations from the trailing arguments for `transcode(::Type{C},
...) where {C<:Codec}` causing a method ambiguity with `transcode(T,
src::String)` in Julia `Base`[^3]. This adds an additional method to
`Base.transcode` to resolve this ambiguity.

Fixes JuliaIO#139.

[^1]: https://github.com/JuliaIO/CodecZlib.jl/tree/f9fddaa28c093c590a7a93358709df2945306bc7#usage
[^2]: https://github.com/JuliaIO/CodecZstd.jl/tree/6327ffa9a3a12fc46d465dcfc8b30bed91cf284b#usage
[^3]: https://github.com/JuliaLang/julia/blob/ff7b8eb00bf887f20bf57fb7e53be0070a242c07/base/c.jl#L306
@bauglir bauglir force-pushed the disambiguate-transcode-for-codec-types-and-strings branch from 52784b6 to 642db71 Compare April 15, 2023 06:30
@bauglir
Copy link
Contributor Author

bauglir commented Apr 15, 2023

In hindsight, using invoke to resolve the ambiguity seems like the more robust option long-term to ensure the implementation stays in sync.

@bauglir
Copy link
Contributor Author

bauglir commented Apr 20, 2023

Any chance this can be merged and released @mkitti?

@mkitti
Copy link
Member

mkitti commented Apr 20, 2023

I would like to go further with this.

We should run Test.detect_ambiguities() and make sure this is empty. Could you add that test?

@mkitti
Copy link
Member

mkitti commented Apr 20, 2023

julia> using Test

julia> using TranscodingStreams

julia> detect_ambiguities(TranscodingStreams)
1-element Vector{Tuple{Method, Method}}:
 (transcode(::Type{C}, args...) where C<:TranscodingStreams.Codec in TranscodingStreams at /root/.julia/packages/TranscodingStreams/5yQuA/src/transcode.jl:33, transcode(T, src::String) in Base at c.jl:306)

@mkitti
Copy link
Member

mkitti commented Apr 20, 2023

Add this somewhere in the tests please.

@test isempty(detect_ambiguities(TranscodingStreams))

@bauglir
Copy link
Contributor Author

bauglir commented Apr 20, 2023

Could you add that test?

Yeah sure! I should be able to add that later today. I had held off on that because that also seems to be the point of #142.

@mkitti
Copy link
Member

mkitti commented Apr 20, 2023

Let's just merge that then.

@mkitti mkitti merged commit eda3444 into JuliaIO:master Apr 20, 2023
@bauglir bauglir deleted the disambiguate-transcode-for-codec-types-and-strings branch April 20, 2023 13:44
@bauglir
Copy link
Contributor Author

bauglir commented Apr 25, 2023

Thanks for getting this merged. Any chance we can get a new release so downstream users do not have to work around this on v0.9.12?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

transcode(::Type{CodecZlib.GzipCompressor}, ::String) is ambiguous on v0.9.12
2 participants