Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
odow committed May 2, 2024
1 parent 75415a3 commit 9ab125e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/src/manual/nonlinear.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ x² - 0.2 x + 0.010000000000000002
julia> typeof(f)
QuadExpr (alias for GenericQuadExpr{Float64, GenericVariableRef{Float64}})
```
To over-ride this behavior, use the [`@force_nonlinear`](@ref) macro:
To override this behavior, use the [`@force_nonlinear`](@ref) macro:
```jldoctest force_nonlinear
julia> g = @force_nonlinear((x - 0.1)^2)
(x - 0.1) ^ 2
Expand Down
17 changes: 10 additions & 7 deletions src/macros/@force_nonlinear.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,14 @@ macro force_nonlinear(expr)
end
return x
end
return quote
r = $(esc(ret))
if !(r isa $GenericNonlinearExpr)
$error_fn("expression did not produce a GenericNonlinearExpr")
end
r
end
return Expr(:call, _force_nonlinear, error_fn, esc(ret))
end

_force_nonlinear(::F, ret::GenericNonlinearExpr) where {F} = ret

function _force_nonlinear(error_fn::F, ret::Any) where {F}
return error_fn(
"expression did not produce a `GenericNonlinearExpr`. Got a " *
"`$(typeof(ret))`: $(ret)",
)
end
2 changes: 1 addition & 1 deletion test/test_macros.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2373,7 +2373,7 @@ function test_force_nonlinear()
@test @force_nonlinear(x^2) isa GenericNonlinearExpr
@test_throws_runtime(
ErrorException(
"In `@force_nonlinear(x)`: expression did not produce a GenericNonlinearExpr",
"In `@force_nonlinear(x)`: expression did not produce a `GenericNonlinearExpr`. Got a `$(typeof(x))`: $x",
),
@force_nonlinear(x),
)
Expand Down

0 comments on commit 9ab125e

Please sign in to comment.