@@ -8,9 +8,9 @@ Representation of a channel passing objects of type `T`.
8
8
abstract type AbstractChannel{T} end
9
9
10
10
"""
11
- Channel{T}(sz ::Int=0)
11
+ Channel{T=Any}(size ::Int=0)
12
12
13
- Constructs a `Channel` with an internal buffer that can hold a maximum of `sz ` objects
13
+ Constructs a `Channel` with an internal buffer that can hold a maximum of `size ` objects
14
14
of type `T`.
15
15
[`put!`](@ref) calls on a full channel block until an object is removed with [`take!`](@ref).
16
16
@@ -24,7 +24,7 @@ Other constructors:
24
24
* `Channel(sz)`: equivalent to `Channel{Any}(sz)`
25
25
26
26
!!! compat "Julia 1.3"
27
- The default constructor `Channel()` and default `sz =0` were added in Julia 1.3.
27
+ The default constructor `Channel()` and default `size =0` were added in Julia 1.3.
28
28
"""
29
29
mutable struct Channel{T} <: AbstractChannel{T}
30
30
cond_take:: Threads.Condition # waiting for data to become available
@@ -51,26 +51,27 @@ function Channel{T}(sz::Float64) where T
51
51
sz = (sz == Inf ? typemax (Int) : convert (Int, sz))
52
52
return Channel {T} (sz)
53
53
end
54
- Channel (sz) = Channel {Any} (sz)
55
- Channel () = Channel {Any} (0 )
54
+ Channel (sz= 0 ) = Channel {Any} (sz)
56
55
57
56
# special constructors
58
57
"""
59
- Channel(func::Function; ctype=Any, csize=0, taskref=nothing)
58
+ Channel{T=Any} (func::Function, size=0; taskref=nothing)
60
59
61
60
Create a new task from `func`, bind it to a new channel of type
62
- `ctype ` and size `csize `, and schedule the task, all in a single call.
61
+ `T ` and size `size `, and schedule the task, all in a single call.
63
62
64
63
`func` must accept the bound channel as its only argument.
65
64
66
65
If you need a reference to the created task, pass a `Ref{Task}` object via
67
- keyword argument `taskref`.
66
+ the keyword argument `taskref`.
68
67
69
68
Return a `Channel`.
70
69
71
70
# Examples
72
71
```jldoctest
73
- julia> chnl = Channel(c->foreach(i->put!(c,i), 1:4));
72
+ julia> chnl = Channel() do ch
73
+ foreach(i -> put!(ch, i), 1:4)
74
+ end;
74
75
75
76
julia> typeof(chnl)
76
77
Channel{Any}
@@ -89,7 +90,9 @@ Referencing the created task:
89
90
```jldoctest
90
91
julia> taskref = Ref{Task}();
91
92
92
- julia> chnl = Channel(c -> println(take!(c)); taskref=taskref);
93
+ julia> chnl = Channel(taskref=taskref) do ch
94
+ println(take!(ch))
95
+ end;
93
96
94
97
julia> istaskdone(taskref[])
95
98
false
@@ -102,11 +105,8 @@ true
102
105
```
103
106
104
107
!!! compat "Julia 1.3"
105
- The following constructors were added in Julia 1.3.
106
-
107
- Other constructors:
108
- * `Channel{T}(func::Function, sz=0)`
109
- * `Channel{T}(func::Function; csize=0, taskref=nothing)`
108
+ This constructor was added in Julia 1.3. Earlier versions of Julia used kwargs
109
+ to set `size` and `T`, but those constructors are deprecated.
110
110
111
111
```jldoctest
112
112
julia> chnl = Channel{Char}(1) do ch
@@ -120,22 +120,21 @@ julia> String(collect(chnl))
120
120
"hello world"
121
121
```
122
122
"""
123
- function Channel (func:: Function ; ctype = Any, csize = 0 , taskref= nothing )
124
- chnl = Channel {ctype} (csize )
123
+ function Channel {T} (func:: Function , size = 0 ; taskref= nothing ) where T
124
+ chnl = Channel {T} (size )
125
125
task = Task (() -> func (chnl))
126
126
bind (chnl, task)
127
127
yield (task) # immediately start it
128
128
129
129
isa (taskref, Ref{Task}) && (taskref[] = task)
130
130
return chnl
131
131
end
132
- function Channel {T} (f:: Function , sz= 0 ) where T
133
- return Channel (f, csize= sz, ctype= T)
134
- end
135
- function Channel {T} (f:: Function ; csize= 0 , taskref= nothing ) where T
136
- return Channel (f, csize= csize, ctype= T, taskref= taskref)
137
- end
132
+ Channel (func:: Function , args... ; kwargs... ) = Channel {Any} (func, args... ; kwargs... )
138
133
134
+ # This constructor is deprecated as of Julia v1.3, and should not be used.
135
+ function Channel (func:: Function ; ctype= Any, csize= 0 , taskref= nothing )
136
+ return Channel {ctype} (func, csize; taskref= taskref)
137
+ end
139
138
140
139
closed_exception () = InvalidStateException (" Channel is closed." , :closed )
141
140
0 commit comments