Skip to content

Commit

Permalink
Merge pull request #1204 from JuliaSymbolics/errorhint
Browse files Browse the repository at this point in the history
add error hint on Num in boolean context
  • Loading branch information
ChrisRackauckas authored Aug 2, 2024
2 parents 77949c9 + 8dbc4b8 commit be988d1
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,22 @@ end
@require SymPy="24249f21-da20-56a4-8eb1-6a02cf4ae2e6" begin
include("../ext/SymbolicsSymPyExt.jl")
end
Base.Experimental.register_error_hint(TypeError) do io, exc
if exc.expected == Bool && exc.got isa Num
println(io,
"\nA symbolic expression appeared in a Boolean context. This error arises in situations where Julia expects a Bool, like ")
printstyled(io, "if boolean_condition", color = :blue)
printstyled(
io, "\t\t use ifelse(boolean_condition, then branch, else branch)\n",
color = :green)
printstyled(io, "x && y", color = :blue)
printstyled(io, "\t\t\t\t use x & y\n", color = :green)
printstyled(io, "boolean_condition ? a : b", color = :blue)
printstyled(io, "\t use ifelse(boolean_condition, a, b)\n", color = :green)
print(io,
"but a symbolic expression appeared instead of a Bool. For help regarding control flow with symbolic variables, see https://docs.sciml.ai/ModelingToolkit/dev/basics/FAQ/#How-do-I-handle-if-statements-in-my-symbolic-forms?")
end
end
end
end

Expand Down

0 comments on commit be988d1

Please sign in to comment.