diff --git a/base/channels.jl b/base/channels.jl index 784a7d6de9781..8f0306ff8f0a9 100644 --- a/base/channels.jl +++ b/base/channels.jl @@ -246,8 +246,9 @@ Append an item `v` to the channel `c`. Blocks if the channel is full. For unbuffered channels, blocks until a [`take!`](@ref) is performed by a different task. """ -function put!(c::Channel, v) +function put!(c::Channel{T}, v) where T check_channel_state(c) + v = convert(T, v) isbuffered(c) ? put_buffered(c,v) : put_unbuffered(c,v) end diff --git a/test/channels.jl b/test/channels.jl index aaaaa69d71931..b750902606d78 100644 --- a/test/channels.jl +++ b/test/channels.jl @@ -43,6 +43,15 @@ end testcpt(32) testcpt(Inf) end + +@testset "type conversion in put!" begin + c = Channel{Int64}(0) + @async put!(c, Int32(1)) + wait(c) + @test isa(take!(c), Int64) + @test_throws MethodError put!(c, "") +end + @testset "multiple for loops waiting on the same channel" begin # Test multiple "for" loops waiting on the same channel which # is closed after adding a few elements.