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
Next Next commit
Add Channel{T}(f::Function) constructors.
The Channel constructors that do and don't create a Task are currently
written in different styles:

 - The non-Task constructor takes a Type {T} as a type parameter, and
 the size as a positional argument.
    - `Channel{Int}(Inf)`
    - `Channel(0)`

 - The Task constructor takes a Function, but the size and type are
keyword arguments:
    - `Channel(c->put!(c,0), ctype=Int, csize=0)`
    - `Channel(ctype=Int, csize=0, taskref=t) do c; put!(c,0); end`

This commit adds convenience methods to the Task-creating constructor,
allowing you to specify the Type and/or size as in the non-Task
constructor:
    - `Channel{Char}(1) do c; put!(c, 'a'); end`
    - `Channel{Char}(csize=2, taskref=t) do c; put!(c, 'a'); end`

---

Also adds tests for the existing Task-creating constructors, which
weren't explicitly tested before.
  • Loading branch information
NHDaly committed Jul 13, 2019
commit abe03ab04d1a3920f75ac68878951a5943379840
22 changes: 22 additions & 0 deletions base/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,22 @@ Hello
julia> istaskdone(taskref[])
true
```

NHDaly marked this conversation as resolved.
Show resolved Hide resolved
Other constructors:
* `Channel{T}(func::Function, sz=0)`
* `Channel{T}(func::Function; csize=0, taskref=nothing)`

```jldoctest
julia> chnl = Channel{Char}(1) do ch
for c in "hello world"
put!(ch, c)
end
end
>> Channel{Char}(sz_max:1,sz_curr:1)

julia> String(collect(chnl))
>> "hello world"
```
"""
function Channel(func::Function; ctype=Any, csize=0, taskref=nothing)
chnl = Channel{ctype}(csize)
Expand All @@ -105,6 +121,12 @@ function Channel(func::Function; ctype=Any, csize=0, taskref=nothing)
isa(taskref, Ref{Task}) && (taskref[] = task)
return chnl
end
function Channel{T}(f::Function, sz=0) where T
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This causes a method overwrite warning. This signature can be Channel{T}(f::Function, sz).

Copy link
Member Author

@NHDaly NHDaly Aug 7, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry! I opened #32818 to fix it!

Thanks, good catch! Sorry I didn't notice (the error comes up while building and it's easy to miss. I'll keep an eye out for that in the future!) :)

return Channel(f, csize=sz, ctype=T)
end
function Channel{T}(f::Function; csize=0, taskref=nothing) where T
return Channel(f, csize=csize, ctype=T, taskref=taskref)
end


closed_exception() = InvalidStateException("Channel is closed.", :closed)
Expand Down
26 changes: 26 additions & 0 deletions test/channels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,32 @@ end
@test_throws InexactError Channel(1.5)
end

@testset "Task constructors" begin
c = Channel(ctype=Float32, csize=2) do c; map(i->put!(c,i), 1:100); end
@test eltype(c) == Float32
@test c.sz_max == 2
@test isopen(c)
@test collect(c) == 1:100

c = Channel{Int}() do c; map(i->put!(c,i), 1:100); end
@test eltype(c) == Int
@test c.sz_max == 0
@test collect(c) == 1:100

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

taskref = Ref{Task}()
c = Channel{Int}(csize=0, taskref=taskref) do c; put!(c, 0); end
@test eltype(c) == Int
@test c.sz_max == 0
@test istaskstarted(taskref[])
@test !istaskdone(taskref[])
take!(c); yield()
@test istaskdone(taskref[])
end

@testset "multiple concurrent put!/take! on a channel for different sizes" begin
function testcpt(sz)
c = Channel{Int}(sz)
Expand Down