Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement: Add helpful (error) message when trying to add scalar + array in 1.0+ #29804

Closed
elcritch opened this issue Oct 25, 2018 · 2 comments
Labels
domain:arrays [a, r, r, a, y, s] domain:error handling Handling of exceptions by Julia or the user

Comments

@elcritch
Copy link

I'm trying to pickup the Julia 1.0 syntax differences. One major gotcha for me was the removal of the convenience scalar +/- array functions in favor of the explicit .+ forms. This surprised me and took me a bit to figure out. It's noted in the NEWS.md for the 0.7 release it's not in the 1.0 NEWS.md nor in the early sections of the Julia manual.

Would it be possible to add a warning or a helpful compiler message in the repl along the lines "you might try .+"? Many people picking up Julia will be coming from Python or Matlab where I believe the default is to broadcast addition/sum. It's a small matter, but if in the first 5 minutes someone is trying out Julia and can't figure out how to add 4 to their array it could be very frustrating.

Alternately, ensuring it's clearly stated the first time array operations are introduced in the manual might work just as well.

julia> 4 + [ 1.0, 2.0 ]
ERROR: MethodError: no method matching +(::Int64, ::Array{Float64,1})
Closest candidates are:
+(::Any, ::Any, ::Any, ::Any...) at operators.jl:502
+(::T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}, ::T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8}) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:53
+(::Union{Int16, Int32, Int64, Int8}, ::BigInt) at gmp.jl:456
...
Stacktrace:
[1] top-level scope at none:0

julia> 4 .+ [ 1.0, 2.0 ]
2-element Array{Float64,1}:
5.0
6.0

@JeffBezanson JeffBezanson added domain:arrays [a, r, r, a, y, s] domain:error handling Handling of exceptions by Julia or the user labels Oct 26, 2018
@JeffBezanson
Copy link
Sponsor Member

Related: #29266

@laborg
Copy link
Contributor

laborg commented Feb 6, 2022

The error message has been implemented in the meantime:

julia> 4 + [ 1.0, 2.0 ]
ERROR: MethodError: no method matching +(::Int64, ::Vector{Float64})
For element-wise addition, use broadcasting with dot syntax: scalar .+ array

@laborg laborg closed this as completed Feb 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
domain:arrays [a, r, r, a, y, s] domain:error handling Handling of exceptions by Julia or the user
Projects
None yet
Development

No branches or pull requests

3 participants