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

add error hint on Num in boolean context #1204

Merged
merged 2 commits into from
Aug 2, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
@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, "booelan_condition ? a : b", color = :blue)

Check warning on line 228 in src/Symbolics.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"booelan" should be "boolean".
ChrisRackauckas marked this conversation as resolved.
Show resolved Hide resolved
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
Loading