Skip to content

Commit

Permalink
in Channel put!, convert value to Channel type
Browse files Browse the repository at this point in the history
`put!(ch::Channel{T}, v)` should convert `v` to type `T`.

This can prevent errors like:

```
julia> c = Channel{Int}(0)
Channel{Int64}(sz_max:0,sz_curr:0)

julia> @async put!(c, :a)
Task (runnable) @0x00007ff5b9d79270

julia> isready(c) && take!(c)
ERROR: TypeError: in take_unbuffered, in typeassert, expected Int64, got Symbol
Stacktrace:
 [1] take_unbuffered(::Channel{Int64}) at ./channels.jl:323
 [2] take!(::Channel{Int64}) at ./channels.jl:306
 [3] top-level scope at none:0
```
  • Loading branch information
tanmaykm committed Sep 8, 2018
1 parent bb7d043 commit 4fc7f69
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -246,9 +246,10 @@ 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)
isbuffered(c) ? put_buffered(c,v) : put_unbuffered(c,v)
vT = convert(T, v)
isbuffered(c) ? put_buffered(c,vT) : put_unbuffered(c,vT)
end

function put_buffered(c::Channel, v)
Expand Down

0 comments on commit 4fc7f69

Please sign in to comment.