Skip to content

Commit 642db71

Browse files
committed
Resolve transcode method ambiguity
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
1 parent 3583abe commit 642db71

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/transcode.jl

+6
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ function Base.transcode(::Type{C}, args...) where {C<:Codec}
4040
end
4141
end
4242

43+
# Disambiguate `Base.transcode(::Type{C}, args...)` above from
44+
# `Base.transcode(T, ::String)` in Julia `Base`
45+
function Base.transcode(codec::Type{C}, src::String) where {C<:Codec}
46+
return invoke(transcode, Tuple{Any, String}, codec, src)
47+
end
48+
4349
_default_output_buffer(codec, input) = Buffer(
4450
initial_output_size(
4551
codec,

test/codecnoop.jl

+5
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,11 @@
213213
output = TranscodingStreams.Buffer(Vector{UInt8}())
214214
@test transcode(Noop(), data, output) === output.data
215215

216+
data = ""
217+
@test String(transcode(Noop, data)) == data
218+
data = "foo"
219+
@test String(transcode(Noop, data)) == data
220+
216221
TranscodingStreams.test_roundtrip_transcode(Noop, Noop)
217222
TranscodingStreams.test_roundtrip_read(NoopStream, NoopStream)
218223
TranscodingStreams.test_roundtrip_write(NoopStream, NoopStream)

0 commit comments

Comments
 (0)