Skip to content

Commit 126ae69

Browse files
committed
Add Channel() constr, defaults to unbufferred channel
Add unit tests for default constructor
1 parent 9a017f1 commit 126ae69

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

base/channels.jl

+6-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Representation of a channel passing objects of type `T`.
88
abstract type AbstractChannel{T} end
99

1010
"""
11-
Channel{T}(sz::Int)
11+
Channel{T}(sz::Int=0)
1212
1313
Constructs a `Channel` with an internal buffer that can hold a maximum of `sz` objects
1414
of type `T`.
@@ -19,8 +19,12 @@ And vice-versa.
1919
2020
Other constructors:
2121
22+
* `Channel()`: default constructor, equivalent to `Channel{Any}(0)`
2223
* `Channel(Inf)`: equivalent to `Channel{Any}(typemax(Int))`
2324
* `Channel(sz)`: equivalent to `Channel{Any}(sz)`
25+
26+
!!! compat "Julia 1.3"
27+
The default constructor `Channel()` was added in Julia 1.3.
2428
"""
2529
mutable struct Channel{T} <: AbstractChannel{T}
2630
cond_take::Threads.Condition # waiting for data to become available
@@ -48,6 +52,7 @@ function Channel{T}(sz::Float64) where T
4852
return Channel{T}(sz)
4953
end
5054
Channel(sz) = Channel{Any}(sz)
55+
Channel() = Channel{Any}(0)
5156

5257
# special constructors
5358
"""

test/channels.jl

+7-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ using Random
1313
end
1414

1515
@testset "various constructors" begin
16+
c = Channel()
17+
@test eltype(c) == Any
18+
1619
c = Channel(1)
1720
@test eltype(c) == Any
1821
@test put!(c, 1) == 1
@@ -31,7 +34,6 @@ end
3134
tvals = Int[take!(c) for i in 1:10^6]
3235
@test pvals == tvals
3336

34-
@test_throws MethodError Channel()
3537
@test_throws ArgumentError Channel(-1)
3638
@test_throws InexactError Channel(1.5)
3739
end
@@ -48,6 +50,10 @@ end
4850
@test c.sz_max == 0
4951
@test collect(c) == 1:100
5052

53+
c = Channel() do c; put!(1); put!("hi") end
54+
@test c.sz_max == 0
55+
@test collect(c) == [1, "hi"]
56+
5157
c = Channel{Int}(Inf) do c; put!(c,1); end
5258
@test eltype(c) == Int
5359
@test c.sz_max == typemax(Int)

0 commit comments

Comments
 (0)