diff --git a/src/nlp_expr.jl b/src/nlp_expr.jl index ca0a03bd782..1a1c144d989 100644 --- a/src/nlp_expr.jl +++ b/src/nlp_expr.jl @@ -1147,9 +1147,16 @@ end function add_to_expression!(f::GenericNonlinearExpr, args...) return error( - "`add_to_expression!` is not supported for expressions of type " * - "`$(typeof(f))` because they cannot be modified in-place. " * - "Instead of `add_to_expression!(expr, args..)`, use " * - "`expr += *(args...)`.", + """ + `add_to_expression!` is not supported for expressions of type + `$(typeof(f))` because they cannot be modified in-place. + Instead of `add_to_expression!(expr, args..)`, use one of the following: + ```julia + expr += *(args...) + # or + import MutableArithmetics as MA + expr = MA.operate!!(MA.add_mul, expr, args...) + ``` + """, ) end diff --git a/test/test_nlp_expr.jl b/test/test_nlp_expr.jl index f01e90f5d19..1e108d3390d 100644 --- a/test/test_nlp_expr.jl +++ b/test/test_nlp_expr.jl @@ -932,10 +932,17 @@ function test_add_to_expression!() y = zero(NonlinearExpr) @test_throws( ErrorException( - "`add_to_expression!` is not supported for expressions of type " * - "`$(typeof(y))` because they cannot be modified in-place. " * - "Instead of `add_to_expression!(expr, args..)`, use " * - "`expr += *(args...)`.", + """ + `add_to_expression!` is not supported for expressions of type + `$(typeof(f))` because they cannot be modified in-place. + Instead of `add_to_expression!(expr, args..)`, use one of the following: + ```julia + expr += *(args...) + # or + import MutableArithmetics as MA + expr = MA.operate!!(MA.add_mul, expr, args...) + ``` + """, ), add_to_expression!(y, 2.0, sin(x)), )