# Can't Iterate on (::Task)

**URL:** <https://discourse.julialang.org/t/cant-iterate-on-task/31480>\
**Category:** General Usage\
**Created:** [November 25, 2019, 9:58am UTC](https://discourse.julialang.org/t/cant-iterate-on-task/31480 "2019-11-25T09:58:22Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)\
**Post date:** [November 25, 2019, 9:58am UTC](https://discourse.julialang.org/t/cant-iterate-on-task/31480/1 "2019-11-25T09:58:22Z")

</div>

i have a package built where i’m calling one function called `prob` with two methods inside a constructor called `QRelaySampler` and after i call it using `QR=QRelaySampler([2 3;1 1],1 ,1, 0.5) ` then `QR.prob(200)` it shows me this following error:

```julia
ERROR: MethodError: no method matching iterate(::Task)
Closest candidates are:
  iterate(::Core.SimpleVector) at essentials.jl:604
  iterate(::Core.SimpleVector, ::Any) at essentials.jl:604
  iterate(::ExponentialBackOff) at error.jl:214

```

the same thing when i call it using the second method ` QR.prob(200,150,2)`

```julia
**ERROR:** MethodError: objects of type Float64 are not callable

Stacktrace:

 [1] **(::getfield(QuantumRelay, Symbol("#prob#6")){Int64,Int64,Array{Float64,2},Array{Float64,2},getfield(QuantumRelay, Symbol("#setc#3")){Array{Float64,2},JuMP.Model},getfield(QuantumRelay, Symbol("#scan#4")){JuMP.Model,Int64,Array{Int64,1},Array{Int64,1},Array{Int64,1},Array{JuMP.Variable,1}},Float64})(** ::Int64, ::Int64, ::Int64 **)** at **/Users/midow/.julia/packages/QuantumRelay/aX3kK/src/QuantumRelay.jl:78**

 [2] top-level scope at **REPL[29]:1**

```

and this is a part of the code:

```julia
function prob(na)
		    @assert count(!iszero, ui2*na) == 0
		    b = T0*na
		    setc(-b)
		    total = 0.0
		    for x in Task(scan)
		        nab = vi2*x + b #the photon numbers for each item in the sum in the note (10)
		        total += prod([c.^complex(n)/factorial(n) for (c, n) in zip(coef, nab)])
		    end
		    return abs(total*omega)^2
		end

        #compute the probability of detection
        #q: the number of photons detectors report
        #na: the number of photons arrived at detector
        #mask: if there is no detector in this channel, mask=0
		function prob(q, na, mask)
		    q0 = round(Int, q.>0)
		    m0 = round(Int, mask)
		    return prod((q0 + (1-2q0).*pdet0(na)).^m0)
		end

```

the code generally for computing the probability for an ideal system for a detector and then for a realistic system.

you can also find the code in Github in this link :

> <https://github.com/marouanehanhasse/Quantum_Relay/blob/master/src/QuantumRelay.jl>

---

<div class="post-metadata">

**Author:** ![pixel27](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pixel27/32/8902_2.png) [@pixel27](https://discourse.julialang.org/u/pixel27)\
**Post date:** [November 25, 2019, 1:22pm UTC](https://discourse.julialang.org/t/cant-iterate-on-task/31480/2 "2019-11-25T13:22:30Z")

</div>

I suspect your problem is:

```julia
for x in Task(scan)

```

That returns a Task, not a channel, did you mean:

```julia
for x in Channel(scan)

```

That will execute scan as a task and pass it a channel which is also returned so you can iterate on it.

---

<div class="post-metadata">

**Author:** ![marouane1994](https://avatars.discourse-cdn.com/v4/letter/m/74df32/32.png) [@marouane1994](https://discourse.julialang.org/u/marouane1994)\
**Post date:** [November 26, 2019, 6:56pm UTC](https://discourse.julialang.org/t/cant-iterate-on-task/31480/3 "2019-11-26T18:56:06Z")

</div>

your idea should work except right now the code rises a `BoundsError` :  
`ERROR: BoundsError: attempt to access 0-element Array{JuMP.Variable,1} at index [1]` and the error occurs in another file of the package ☹😓  
😭  
[https://github.com/marouanehanhasse/Quantum\_Relay/blob/master/src/scan.jl](https://github.com/marouanehanhasse/Quantum_Relay/blob/master/src/scan.jl)
