Skip to content

vec[1 +1] gives unhelpful error #49676

@LilithHafner

Description

@LilithHafner

I ran across this unhelpful error message

julia> vec = [1, 2, 3]
3-element Vector{Int64}:
 1
 2
 3

julia> vec[1+1]
2

julia> vec[1+ 1]
2

julia> vec[1 + 1]
2

julia> vec[1 +1]
ERROR: MethodError: no method matching typed_hcat(::Vector{Int64}, ::Int64, ::Int64)

Closest candidates are:
  typed_hcat(::Type{T}, ::Number...) where T
   @ Base abstractarray.jl:1614
  typed_hcat(::Type{T}, ::Union{Number, LinearAlgebra.Adjoint{T, <:AbstractVector} where T}...) where T
   @ LinearAlgebra ~/.julia/juliaup/julia-1.9.0-rc3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/LinearAlgebra/src/adjtrans.jl:352
  typed_hcat(::Type{T}, ::Union{Number, LinearAlgebra.Transpose{T, <:AbstractVector} where T}...) where T
   @ LinearAlgebra ~/.julia/juliaup/julia-1.9.0-rc3+0.aarch64.apple.darwin14/share/julia/stdlib/v1.9/LinearAlgebra/src/adjtrans.jl:353
  ...

Stacktrace:
 [1] top-level scope
   @ REPL[54]:1

This is because typed comprehensions Float64[1 2] and array access vec[3] share the same syntax. Giving a hint when the "type" used in a typed comprehension is not a type and is an indexable object might help.

Metadata

Metadata

Assignees

No one assigned

    Labels

    error messagesBetter, more actionable error messagesgood first issueIndicates a good issue for first-time contributors to Julia

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions