Replies: 2 comments 2 replies
-
I think this is an important discussion! But I don't quite understand how your proposed syntax would work. First, I think it is important to note that our syntax is not limited to one expression per initializer. At least in C++, we can write Probably it would also help to describe the concrete problems you currently have with the existing syntax in the Rust target. |
Beta Was this translation helpful? Give feedback.
-
Based on our discussion on Tuesday, it seems that having list expressions as first order citizens in LF as proposed by @oowekyala is likely to cause more issues than they would solve. For the time being, we can just escape lists in Rust initializers with |
Beta Was this translation helpful? Give feedback.
-
Currently initializers are written like this
state name: type(...)
, where what's inside the parentheses may be in the general case a (non-empty) list of expressions.If the variable has a non-array type, then only a single expression is allowed. Then, the variable is assigned to this expression: eg
state name: type (expr)
translates to egtype name = expr
in C.But for variables with array types, the initialization syntax is very inconsistent across targets, and pretty irregular if you ask me, as we have to fall back on using
{= =}
in some cases.For instance in C:
state arr: int[](1, 2)
translates toint name[] = { 1, 2 };
state arr: int[](1)
translates toint name[] = 1;
which is invalid C code.{= { 0 } =}
.The same kind of rules apply to (at least) Python.
I think this design lacks ergonomy for an end-user, because in any language, adding or removing an element from a list is usually done just by deleting/adding a comma and the element. This is an O(1), simple operation which does the same thing regardless of the final size of the list. In LF, there's a sharp edge when you remove an element and the list ends up being of size 1. If you don't wrap your remaining element in eg
{= [ ... ] =}
you may end up with a runtime type error: eg in Python, your state var will not be a list anymore. Similarly, when adding an element, why would one turn({= {0} =})
into(0, 1)
instead of({= {0, 1} =})
if it's unclear why there are two different syntaxes...Moreover, the implementation of code generators is more complicated as it is, as it would be with a consistent syntax to represent array values.
I think we should consider adding LF syntax for a "list literal", and changing the syntax of initializers in the following way:
So an initializer would always be exactly one expression, which would simplify code generators. The expression may be a list literal, like
[1, 2]
or[1 msec, 2 msec]
or[]
, which is translated to language-specific syntax, eg[1, 2]
orstd::vector<int> { 1, 2 }
.See also the very relevant discussion in #399
Edit: so using this syntax would look like so:
state arr: int[] ([1, 2])
. Eg in Python or C, this clarifies the difference betweenstate arr([1 msec])
(which creates a python list/ C array) andstate arr(1 msec)
(which creates a target-specific time value).Beta Was this translation helpful? Give feedback.
All reactions