@variables parsing breaks on default value?

This code snippet:

sts = @variables begin
    m_flow(t), [connect=Flow],
    T(t)=T0, [connect=Stream]
end

Fails with:

LoadError: AssertionError: @variables expects a tuple of expressions or an expression of a tuple (`@variables x y z(t) v[1:3] w[1:2,1:4]` or `@variables x y z(t) v[1:3] w[1:2,1:4] k=1.0`)

This issue goes away when =T0 is removed,

Note that:

@variables T(t)=T0, [connect=Stream]

also fails, but with a different error:

syntax: misplaced assignment statement in "[connect = Stream]" around In[2]:1

without the comma, works fine:

@variables T(t)=T0 [connect=Stream]

However, in this format (without the comma) doesn’t work in a multiline @variables declaration:

@variables begin
    m_flow(t) [connect=Flow]
    T(t)=T0 [connect=Stream]
end
# syntax: space before "[" not allowed in "m_flow(t) [" at In[3]:2

Is this a bug? or is there a correct way to define these variables?

DD

Those commas are all incorrect. That use of commas isn’t anywhere in the docs right?

The last one should work and looks like a parsing error. Open an issue on that one. We should make a multiline @variables with metadata and add it to the docs with a doctest in:

https://mtk.sciml.ai/v8.14/basics/ContextualVariables/#Variable-metadata-[Experimental/TODO]

As the docs say, extensive use of the metadata is still a bit “Experimental/TODO”, but close enough to be in the uncanny valley.

This is from the docs

@variables t [unit = u"s"] x(t) [unit = u"m"] g(t) w(t) [unit = "Hz"]

@variables(t, [unit = u"s"], x(t), [unit = u"m"], g(t), w(t), [unit = "Hz"])

and this is from a test

@variables begin
    x = [1, 2], [connect = Flow, unit = u]
    y = 2
end

However, seems I’ve added an extra comma. That said, it doesn’t seem to matter:

sts = @variables begin
    m_flow(t), [connect=Flow]
    T(t)=T0, [connect=Stream]
end
#syntax: misplaced assignment statement in "[connect = Stream]" around In[5]:2

which is weird, since this is very similar to the test above.

That’s just how Julia parses macros. If you do @macro() you have to put commas for the arguments, but if it’s not used as a function it’s done by space.

Yeah, looks like you found something the test didn’t. That’s worth an issue.