Range element wise product

Trying to multiply a range with an array gave a no method matching error

1:4 .* [1,2,3,4]

ERROR: MethodError: no method matching (::Colon)(::Int64, ::Array{Int64,1})

Rather than accepting it as range it takes in as Int64 type. Why does it fail to work in this way ?

it seems to work with range in parenthesis

(1:4) .* [1,2,3,4]

This is because the code is being read as 1:(4 .* [1,2,3,4]).

You can investigate this yourself by quoting and dumping the code:


julia> ex = :(1:4 .* [1,2,3,4])
:(1:4 .* [1, 2, 3, 4])

julia> dump(ex)
Expr
  head: Symbol call
  args: Array{Any}((3,))
    1: Symbol :
    2: Int64 1
    3: Expr
      head: Symbol call
      args: Array{Any}((3,))
        1: Symbol .*
        2: Int64 4
        3: Expr
          head: Symbol vect
          args: Array{Any}((4,))
            1: Int64 1
            2: Int64 2
            3: Int64 3
            4: Int64 4
6 Likes