# Weird error, very lost

**URL:** <https://discourse.julialang.org/t/weird-error-very-lost/298>\
**Category:** General Usage\
**Created:** [November 14, 2016, 6:01pm UTC](https://discourse.julialang.org/t/weird-error-very-lost/298 "2016-11-14T18:01:28Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bdeonovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdeonovic/32/3928_2.png) [@bdeonovic](https://discourse.julialang.org/u/bdeonovic)\
**Post date:** [November 14, 2016, 6:01pm UTC](https://discourse.julialang.org/t/weird-error-very-lost/298/1 "2016-11-14T18:01:28Z")

</div>

For some reason `Y[idx1,c]` and `Y[idx2,c]` are returning single, large Ints (even though idx1 and idx2 are ranges).

This does not happen if I run the code manually (run each line in julia REPL, rather than calling function). What is happening?

I can reproduce with random data (MWE):

```julia
Y=reshape(round(Int, abs(randn(5*1000)))+1,1000,5)

function FOO(Y::AbstractMatrix; frac::Float64=0.3, nbins::Int=100, n_sims::Int=100)
  ## setup temp vars
  num_iters, num_chains = size(Y)

  #start_iters = collect(1:round(Int64, num_iters/(2*nbins)):(round(Int64, num_iters/2)))
  start_iters = unique([1; [round(Int64, s) for s in logspace(log(10,100),log(10,num_iters/2),nbins-1)]])
  result = zeros(Float64, 10, length(start_iters) * num_chains)
  j=1
  for c in 1:num_chains
    for st in 1:length(start_iters)
      n = length(start_iters[st]:num_iters)
      println(STDERR, start_iters[st]:round(Int64, start_iters[st] + frac * n - 1))
      println(STDERR, round(Int64, num_iters - frac * n + 1):num_iters)
      idx1 = start_iters[st]:round(Int64, start_iters[st] + frac * n - 1)
      idx2 = round(Int64, num_iters - frac * n + 1):num_iters
      y1 = Y[idx1,c]
      y2 = Y[idx2,c]
      println(y1)
      println(y2)
      n_min = min(length(y1), length(y2))
      println(n_min)
      X = [y1[1:n_min] y2[(end - n_min + 1):end]]
    end
  end
end

julia> FOO(Y)
1:300
701:1000
214700992
213779136
1
ERROR: MethodError: no method matching getindex(::Int64, ::UnitRange{Int64})
 in #FOO#24(::Float64, ::Int64, ::Int64, ::Function, ::Array{Int64,2}, ::Int64) at ./REPL[53]:22
 in FOO(::Array{Int64,2}, ::Int64) at ./REPL[53]:3

```

If i comment out last line of `FOO` (where `X` is set), the function runs. Whats up? Also if I change

```julia
start_iters = unique([1; [round(Int64, s) for s in logspace(log(10,100),log(10,num_iters/2),nbins-1)]])

```

to something like

```julia
start_iters=1:num_iters

```

it works

```julia
Julia Version 0.5.0
Commit 3c9d753 (2016-09-19 18:14 UTC)
Platform Info:
  System: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU E5-2650 v2 @ 2.60GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.7.1 (ORCJIT, ivybridge)

```

---

<div class="post-metadata">

**Author:** ![mbauman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mbauman/32/31082_2.png) [@mbauman](https://discourse.julialang.org/u/mbauman)\
**Post date:** [November 14, 2016, 10:26pm UTC](https://discourse.julialang.org/t/weird-error-very-lost/298/2 "2016-11-14T22:26:02Z")

</div>

This looks like a bug with inference. I reduced it a bit further:

```nohighlight
julia> function FOO(Y::AbstractMatrix)
         frac=.3
         nbins=100
         n_sims=100
         num_iters, num_chains = size(Y)

         start_iters = unique([1; [round(Int64, s) for s in logspace(log(10,100),log(10,num_iters/2),nbins-1)]])
         c=1
         for st in 1:length(start_iters)
             n = length(start_iters[st]:num_iters)
             idx1 = start_iters[st]:round(Int64, start_iters[st] + frac * n - 1)
             y1 = Y[idx1,c]
             return y1[1:end]
         end
       end

```

If you look at how it gets typed, you can see that it thinks that the result of indexing `y1` by an unknown index type is an `Int`:

```nohighlight
y1::Int64 = (Main.getindex)(Y::Array{Int64,2},idx1::Any,c::Int64)::Int64 #

```

This should be `y1::Any`. Would you be willing to submit a bug report on the issue tracker? In the meantime, you can work around it by asserting `start_iters = unique(...)::Vector{Int}`.

---

<div class="post-metadata">

**Author:** ![bdeonovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bdeonovic/32/3928_2.png) [@bdeonovic](https://discourse.julialang.org/u/bdeonovic)\
**Post date:** [November 14, 2016, 11:21pm UTC](https://discourse.julialang.org/t/weird-error-very-lost/298/3 "2016-11-14T23:21:46Z")

</div>

Thanks, I never had to deal with issues with type inference before. Issue open at

[https://github.com/JuliaLang/julia/issues/19322](https://github.com/JuliaLang/julia/issues/19322)

---

<div class="post-metadata">

**Author:** ![Ralph\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ralph_smith/32/10344_2.png) [@Ralph\_Smith](https://discourse.julialang.org/u/Ralph_Smith)\
**Post date:** [November 15, 2016, 3:02am UTC](https://discourse.julialang.org/t/weird-error-very-lost/298/4 "2016-11-15T03:02:01Z")

</div>

If you need a work-around, type coercion seems to avoid the bug:

```julia
idx1::UnitRange = start_iters[st]:round(Int64, start_iters[st] + frac * n - 1)
idx2::UnitRange = round(Int64, num_iters - frac * n + 1):num_iters

```
