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

Add Channel{T}(f::Function) constructors. #30855

Merged
merged 6 commits into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Channel{Int}() constructor, default to sz=0
  • Loading branch information
NHDaly committed Jul 13, 2019
commit 18a80ae7060138c0f30d795f2a8ba1376feda2e9
4 changes: 2 additions & 2 deletions base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Other constructors:
* `Channel(sz)`: equivalent to `Channel{Any}(sz)`

!!! compat "Julia 1.3"
The default constructor `Channel()` was added in Julia 1.3.
The default constructor `Channel()` and default `sz=0` were added in Julia 1.3.
"""
mutable struct Channel{T} <: AbstractChannel{T}
cond_take::Threads.Condition # waiting for data to become available
Expand All @@ -36,7 +36,7 @@ mutable struct Channel{T} <: AbstractChannel{T}
data::Vector{T}
sz_max::Int # maximum size of channel

function Channel{T}(sz::Integer) where T
function Channel{T}(sz::Integer = 0) where T
if sz < 0
throw(ArgumentError("Channel size must be either 0, a positive integer or Inf"))
end
Expand Down
5 changes: 5 additions & 0 deletions test/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ end
@testset "various constructors" begin
c = Channel()
@test eltype(c) == Any
@test c.sz_max == 0

c = Channel(1)
@test eltype(c) == Any
Expand All @@ -28,6 +29,10 @@ end
@test eltype(c) == Int
@test_throws MethodError put!(c, "Hello")

c = Channel{Int}()
@test eltype(c) == Int
@test c.sz_max == 0

c = Channel{Int}(Inf)
@test eltype(c) == Int
pvals = map(i->put!(c,i), 1:10^6)
Expand Down