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() constr, defaults to unbufferred channel
Add unit tests for default constructor
  • Loading branch information
NHDaly committed Jul 13, 2019
commit d5cad7464b19636f856a6e386acd356bdd7cb8ec
7 changes: 6 additions & 1 deletion base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Representation of a channel passing objects of type `T`.
abstract type AbstractChannel{T} end

"""
Channel{T}(sz::Int)
Channel{T}(sz::Int=0)

Constructs a `Channel` with an internal buffer that can hold a maximum of `sz` objects
of type `T`.
Expand All @@ -19,8 +19,12 @@ And vice-versa.

Other constructors:

* `Channel()`: default constructor, equivalent to `Channel{Any}(0)`
* `Channel(Inf)`: equivalent to `Channel{Any}(typemax(Int))`
* `Channel(sz)`: equivalent to `Channel{Any}(sz)`

!!! compat "Julia 1.3"
The default constructor `Channel()` was added in Julia 1.3.
"""
mutable struct Channel{T} <: AbstractChannel{T}
cond_take::Threads.Condition # waiting for data to become available
Expand Down Expand Up @@ -48,6 +52,7 @@ function Channel{T}(sz::Float64) where T
return Channel{T}(sz)
end
Channel(sz) = Channel{Any}(sz)
Channel() = Channel{Any}(0)

# special constructors
"""
Expand Down
8 changes: 7 additions & 1 deletion test/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ using Random
end

@testset "various constructors" begin
c = Channel()
@test eltype(c) == Any

c = Channel(1)
@test eltype(c) == Any
@test put!(c, 1) == 1
Expand All @@ -31,7 +34,6 @@ end
tvals = Int[take!(c) for i in 1:10^6]
@test pvals == tvals

@test_throws MethodError Channel()
@test_throws ArgumentError Channel(-1)
@test_throws InexactError Channel(1.5)
end
Expand All @@ -48,6 +50,10 @@ end
@test c.sz_max == 0
@test collect(c) == 1:100

c = Channel() do c; put!(c, 1); put!(c, "hi") end
@test c.sz_max == 0
@test collect(c) == [1, "hi"]

c = Channel{Int}(Inf) do c; put!(c,1); end
@test eltype(c) == Int
@test c.sz_max == typemax(Int)
Expand Down