Hi Mark,
what do the two types of quotes (double and single) do?
It’s just like Python: There’s no semantic difference between the two. It’s just a matter of preference or convenience (e.g. when you want a string with embedded quotes of one type, delimit the string with the other type).
What does the semicolon do?
If you want to specify multiple component parameters on a single line, you separate them with semicolons.
I see in some variable defns you have var sync (default_value = 0)
and others have
var sync = 0
. Difference?
The second is shorthand for the first.
The first form is an example of the general component declaration syntax, which you must use if you want to specify more than just the default value for a variable.
In variable definitions, are two quotes are needed for strings: “‘random_without_replacement’” etc. ?
They aren’t in assignment-style variable declarations:
var a = 'Hello, world!'
However, they are needed when you use component-declaration syntax:
var a (default_value = "'Hello, world!'")
The reason for this is that the MWEL-to-XML translator strips quotes from parameter values. This is needed because most MWorks components expect to receive “raw” text (and not quoted strings) for their parameters. For example,
run_python_string ('x = 3')
is converted to
<action type="run_python_string" code="x = 3"/>
If the translator didn’t strip the quotes, then the XML would contain code="'x = 3'"
, and the Python “code” would be 'x = 3'
(i.e. a string literal). Does that make sense?
The basic issue is that, in general, the MWEL-to-XML translator doesn’t know whether a parameter value will be treated as an expression or as “raw” text. However, in the case of assignment-style variable declarations (and also actual assignments), it does know that the value to the right of =
is an expression; if the value is a quoted string, the quotes are not stripped.
I know this a wart, but I don’t see a good way to avoid it.
Do you have a code highlighting style you use? Javascript? C with linux-style braces?
Do you mean a code formatting style?
MWEL doesn’t give you as much freedom as C-like languages. For example, every statement must end with a newline, and newlines are not permitted between the signature/tag and the parameter list, or between the parameter list and children, in component declarations. In other words, this is correct:
if (x) {
report ('x is true')
}
but these are invalid syntax:
if // ERROR: parameter list can't start on next line
(x) {
report ('x is true')
}
if (x) // ERROR: children can't start on next line
{
report ('x is true')
}
However, unlike Python, indentation is not significant.
I like the formatting style I use in the docs and in the examples I’ve sent you, but you’re free to come up with your own (within the limits of the syntax).
If you’re asking about syntax highlighting modes in a text editor, I have a very rudimentary MWEL mode for Sublime Text. It doesn’t do much at the moment, but I’m hoping to make it better (eventually).
It appears that expressions to the right of equals signs are sometimes interpreted differently: when = timer_expired(t)
, duration_units = ms
, variable1 = variable2
. Are these all part of a unified expression language that I’m not putting together?
It’s very similar to how Python uses =
for both variable assignments and keyword arguments in function calls. This assigns the value of variable y
to variable x
:
x = y
Whereas this declares a goto transition with parameters target
and when
:
goto (
target = NextState
when = timer_expired(MyTimer)
)
// Equivalent:
goto (target = NextState; when = timer_expired(MyTimer))
Chris