diff --git a/base/channels.jl b/base/channels.jl index f3b53188a791f..a9e060663e801 100644 --- a/base/channels.jl +++ b/base/channels.jl @@ -304,14 +304,19 @@ 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. +Return the first argument. !!! compat "Julia 1.1" `v` now gets converted to the channel's type with [`convert`](@ref) as `put!` is called. + +!!! compat "Julia 1.4" + `put!` now returns the channel `c`; older versions of Julia returned `v`. """ function put!(c::Channel{T}, v) where T check_channel_state(c) v = convert(T, v) - return isbuffered(c) ? put_buffered(c, v) : put_unbuffered(c, v) + isbuffered(c) ? put_buffered(c, v) : put_unbuffered(c, v) + return c end function put_buffered(c::Channel, v) diff --git a/test/channels.jl b/test/channels.jl index fc5852df62dbc..e172725a7a445 100644 --- a/test/channels.jl +++ b/test/channels.jl @@ -19,7 +19,7 @@ end c = Channel(1) @test eltype(c) == Any - @test put!(c, 1) == 1 + @test put!(c, 1) === c @test isready(c) == true @test take!(c) == 1 @test isready(c) == false @@ -35,7 +35,8 @@ end c = Channel{Int}(Inf) @test eltype(c) == Int - pvals = map(i->put!(c,i), 1:10^6) + pvals = 1:10^6 + foldl(put!, pvals; init=c) tvals = Int[take!(c) for i in 1:10^6] @test pvals == tvals @@ -162,7 +163,7 @@ using Distributed if N > 0 for i in 1:5 - @test put!(cs[i], 2) === 2 + @test put!(cs[i], 2) === cs[i] end end for i in 1:5 @@ -456,3 +457,8 @@ let t = @async nothing wait(t) @test_throws ErrorException("schedule: Task not runnable") schedule(t, nothing) end + +@testset "push!(c, v) -> c" begin + c = Channel(Inf) + @test push!(c, nothing) === c +end