From 3bb2162d3979513e7156f8de40bccf9d9dbba059 Mon Sep 17 00:00:00 2001 From: Fredrik Bagge Carlson Date: Fri, 2 Aug 2024 14:36:39 +0200 Subject: [PATCH 1/2] add error hint on Num in boolean context --- src/Symbolics.jl | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/Symbolics.jl b/src/Symbolics.jl index 754e609a3..cbd20f32c 100644 --- a/src/Symbolics.jl +++ b/src/Symbolics.jl @@ -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, "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 From 8dbc4b8769047c3851f0736b65741ba9879eee8b Mon Sep 17 00:00:00 2001 From: Christopher Rackauckas Date: Fri, 2 Aug 2024 08:41:55 -0400 Subject: [PATCH 2/2] Update src/Symbolics.jl --- src/Symbolics.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symbolics.jl b/src/Symbolics.jl index cbd20f32c..8d88e1470 100644 --- a/src/Symbolics.jl +++ b/src/Symbolics.jl @@ -225,7 +225,7 @@ end 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, "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?")