Skip to content

Cannot broadcast when rand(n) is involved #32744

@giordano

Description

@giordano

I think the title is not super clear, but the example below should be:

julia> @. rand(2) + 2.4
ERROR: MethodError: no method matching +(::Array{Float64,1}, ::Float64)
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:502
  +(::Bool, ::T<:AbstractFloat) where T<:AbstractFloat at bool.jl:112
  +(::Float64, ::Float64) at float.jl:395
  ...
Stacktrace:
 [1] _broadcast_getindex_evalf at ./broadcast.jl:578 [inlined]
 [2] _broadcast_getindex at ./broadcast.jl:551 [inlined]
 [3] getindex at ./broadcast.jl:511 [inlined]
 [4] copy at ./broadcast.jl:763 [inlined]
 [5] materialize(::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0},Nothing,typeof(+),Tuple{Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{0},Nothing,typeof(rand),Tuple{Int64}},Float64}}) at ./broadcast.jl:753
 [6] top-level scope at none:0

@chethega pointed out on Slack that this is not surprising by how Meta.@lower works, but I find the error message quite confusing. Also, rand.(2) generates a Vector, as rand(2) does, but rand(2) .+ 2.4 works without problems.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions