Conversation

cstjean

Fix #3640

Incidentally, include("test/model_parsing.jl") at the REPL fails with

ERROR: LoadError: IOError: stat("//www.w3.org/2000\\svg\" width=\"80\" height=\"30\">\n<path d=\"M10 15\nl15 0\nl2.5 -5\nl5 10\nl5 -10\nl5 10\nl5 -10\nl5 10\nl2.5 -5\nl15 0\" stroke=\"black\" stroke-width=\"1\" stroke-linejoin=\"bevel\" fill=\"none\"><\\path>\n<\\svg>\n"): unknown error (UNKNOWN)

on Windows. Is there an easy fix for that?

push!(exprs.args, :(systems = ModelingToolkit.AbstractSystem[]))
push!(exprs.args, :(equations = Union{Equation, Vector{Equation}}[]))
push!(exprs.args, :(defaults = Dict{Num, Union{Number, Symbol, Function}}()))
push!(exprs.args, :(systems = $MTK.AbstractSystem[]))

Choose a reason for hiding this comment

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

Is prepending $MTK. to everything really the solution? What if we did this instead:

Suggested change
push!(exprs.args, :(systems = $MTK.AbstractSystem[]))
push!(exprs.args, :(systems = $AbstractSystem[]))

Also wouldn't this need to be done in more places? e.g. where @variables is called.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

See the discussion around https://discourse.julialang.org/t/adding-method-to-function-in-macro/128613/6 . If you're going to esc the expression, then you need either $MTK.AbstractSystem or $AbstractSystem, but there are places where the latter will fail. In particular, you can't $@some_macro obviously. I always use the former style for that reason, but I don't mind changing this PR to $AbstractSystem if that's your preference.

Also wouldn't this need to be done in more places?

Yes, absolutely. I didn't want to venture outside of my MTK comfort zone.

e.g. where @variables is called.

?

Choose a reason for hiding this comment

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

See the discussion around https://discourse.julialang.org/t/adding-method-to-function-in-macro/128613/6 . If you're going to esc the expression, then you need either $MTK.AbstractSystem or $AbstractSystem, but there are places where the latter will fail

I see, thanks for clarifying. $MTK is fine, then.

e.g. where @variables is called.

?

While parsing @variables inside @mtkmodel, we generate code that calls Symbolics.@variables and MTK.@parameters. I assume those need to be qualified as well?

Copy link
Contributor Author

@cstjean cstjean May 20, 2025

Choose a reason for hiding this comment

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

Yes, I always prefix functions, types and macros with $MyModule. when using esc. It's the flip side of skipping macro hygiene. It can also prevent unnecessary conflicts if ModelingToolkit and SomeOtherLibrary export the same symbol.

In this particular case it doesn't matter because the user will feel compelled to import ModelingToolkit: @variables anyway, but that's not reliable in general.

Sign up for free to join this conversation on . Already have an account? Sign in to comment
None yet
None yet

Successfully merging this pull request may close these issues.

@mtkmodel macro requires imports from MTK