From cb1c8678e2fad85e996405ec2f2e57704b6ac611 Mon Sep 17 00:00:00 2001 From: Merry Date: Sun, 26 Jun 2022 15:04:47 +0100 Subject: [PATCH] errorshow: Show hint when + is attempted on AbstractStrings --- base/errorshow.jl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/base/errorshow.jl b/base/errorshow.jl index aaf040cd71b8d7..b0910844ef9b75 100644 --- a/base/errorshow.jl +++ b/base/errorshow.jl @@ -913,6 +913,22 @@ end Experimental.register_error_hint(noncallable_number_hint_handler, MethodError) +# Display a hint in case the user tries to call the instance of a number +# (probably missing the operator) +# eg: (1 + 2)(3 + 4) +# Display a hint in case the user tries to use the + operator on strings +# (probably attempting concatenation) +function string_concatenation_hint_handler(io, ex, arg_types, kwargs) + @nospecialize + if (ex.f == +) && all(i -> i <: AbstractString, arg_types) + print(io, "\nString concatenation is performed with ") + printstyled(io, "*", color=:cyan) + print(io, " (See also: https://docs.julialang.org/en/v1/manual/strings/#man-concatenation).") + end +end + +Experimental.register_error_hint(string_concatenation_hint_handler, MethodError) + # ExceptionStack implementation size(s::ExceptionStack) = size(s.stack) getindex(s::ExceptionStack, i::Int) = s.stack[i]