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

Error hint on Num in boolean context #2919

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
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
15 changes: 15 additions & 0 deletions src/ModelingToolkit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,19 @@
export Sample, Hold, Shift, ShiftIndex, sampletime, SampleTime
export Clock, SolverStepClock, TimeDomain

function __init__()
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 286 in src/ModelingToolkit.jl

View workflow job for this annotation

GitHub Actions / Spell Check with Typos

"booelan" should be "boolean".
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?")
Comment on lines +281 to +288
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
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)
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?")
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)
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 # module
Loading