You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Native VAR syntax to create variables inside tests and keywords #3761
I would also consider fish shell style Set my_variable value syntax. So just keyword Set for setting a variable to a value. Wouldn't require any special syntax.
But this := also seems like a significant upgrade.
Basically my point is, I don't think the short-hand needs to be := but some shorthand is necessary and this is a fine option.
would be a single token i.e. just one space around :=? If yes, that would have a problem that it couldn't be lexed using the current general parsing rules and e.g. LSP needed to handle it separately. It wouldn't be compatible with how execution is logged either so it would require quite a lot of changes. I'd also use just = instead of := (with required spaces around) because Python uses the "walrus operator" for assignment expression syntax and it behaves a bit differently.
The idea to use
Set var value
by @xylix is actually already mostly supported by the newish
Set Local Variable ${var} value
but obviously this is a bit more verbose syntax. I still don't think adding yet another keyword would help and using something as generic as Set would also likely conflict with many keywords users have in their libraries.
The approach proposed by @Snooz82 to allow using = as a keyword name like
${var} = value
is actually something I've been thinking about earlier. It would have a great benefit that we just needed to make = an alias for Set Variable (easy!) and then everything else ought to work just fine. Well, probably in some string representations and in the log file we needed to make sure that the assignment would look like ${var} = value and not like ${var} = = value (i.e. the = automatically added with assignments shouldn't be added). A related problem would be that you could then in the data have
${var} = = value
but I don't consider that a big problem. You just don't need to use such stupid syntax. Another problem is that
${var} = xxx
${var} = xxx
would look pretty similar but in the first case xxx is a keyword and in the latter case it's a value to assign directly. I don't think that's a big problem either.
If others agree, I'd be fine going with the above syntax and consider adding it already in RF 4. There would still require some design decisions related to how to handle lists and dicts but if that gets complicated just supporting the scalar assignment would already help a lot.
@boakley, it sounds great to me! The confusion among ':=' and that existing optional '=' would be lower than in case of "keyword ="; and I think the same for case of equality comparison operator '=='. I came across opinion that simple '=' used for assigning is confusing, since people know equal sign as a comparison operator: ${a} = 5 evoke "a is equal to 5" more than "set a to 5". (I think this is the reason Pascal language, aimed at beginners in programming, used ':=' for assigning variables as well.)
No consensus on the syntax to use. Moving to RF 5.1 to give us more time to think about this.
We have also been thinking about inline keyword call syntax (#3187) and it should be taken into account when designing this syntax. These two features are interestingly related. A problem with variable assignment syntax is that in ${x} = something syntax that something is a keyword and we'd like it to be a literal value. With the inline keyword call syntax the problem is opposite: in something value that value is an argument and we wanted it to be a keyword.
changed the title [-]Native `VAR` syntax to create variables insinde tests/tasks and keywords[/-][+]Native `VAR` syntax to create variables inside tests and keywords[/+]on Oct 23, 2023
It is now possible to have = after the variable name the same way as when creating variables based on keyword return values and in the Variables section. This was requested by users on our Slack and implemented in 3625814.
Option parsing now accepts each option only once. For example,
VAR &{x} scope=value scope=test
now creates a variable with value {'scope': 'value'} when earlier scope was considered to be an option twice which caused an error.
We had a long discussion about the conditional VAR syntax on our Slack. We had a vote about syntax alternatives I explained in an earlier comment and the
VAR ${x} y IF $cond ELSE z
variant was generally preferred.
Things got more complicated when I realized that this kind of conditional assignment could be useful also in the Variables section. In that usage ELSE IF support would likely be useful, but the above syntax cannot really be extended to handle it. It could thus be that the
VAR ${x} IF $cond y ELSE z
variant that would also support ELSE IF (see the aforementioned comment for an example) could be a better syntax choice.
In the end it turned out that it is best to post-pone adding the conditional assignment support to some future release. This is why:
The VAR syntax is useful already in its current form.
It can be used with IF, including inline IF, to set variables conditionally.
As I explained in the above comment, it's not clear which syntax would be best.
Implementing conditional assignment support seems to be considerably more complicated than I initially thought.
Especially the ELSE IF variant would be challenging to implement.
We are running out of the budget with RF 7.0.
There are more important features in RF 7.0 scope than conditional VAR support.
After this decision the only remaining task related to the VAR syntax in RF 7.0 ought to be documentation. Once that's ready, I think we are ready for an alpha release that makes it easier for users to test this new syntax.
Documentation is now done and this issue ought to be ready. Looking forward for feedback once the alpha is out! At this point of the RF 7 development we can still make adjustments to the syntax if needed.
If we were to support conditional assignment with VAR, we needed to specify the syntax. There are at least two alternatives discussed below:
VAR ${x} y IF $cond ELSE z
This is same structure as with Python's ternary if/else. It would thus be familiar for people with Python skills and would also help people moving from Robot syntax to Python.
VAR ${x} IF $cond y ELSE z
This is close to our inline IF construct. Unlike the above, this would allow ELSE IF as well:
VAR ${x} IF $c > 0 a ELSE IF $c < 0 b ELSE c
I don't know is ELSE IF support really needed, though. It easily gets pretty complicated and would typically be better changed to a normal IF/ELSE. Supporting unlimited branches would also be a lot bigger task than adding support for an optional condition and a false value.
Comments about these alternatives and possible new alternatives are appreciated!
I would strongly vote for the second syntax. There are two reasons for this:
It is more Robotish, since Set Variable Ifand Run Keyword If has similar structure (i.e. there always comes the condition before the appropriate value);
I would use ELSE IF support for conditional settings of variables instead of Set Variable If.
Activity
xylix commentedon Nov 15, 2020
I would also consider fish shell style
Set my_variable valuesyntax. So just keywordSetfor setting a variable to a value. Wouldn't require any special syntax.But this
:=also seems like a significant upgrade.Basically my point is, I don't think the short-hand needs to be
:=but some shorthand is necessary and this is a fine option.[-]Short cut syntax `:=` for `Set Variable`[/-][+]Shortcut syntax `:=` for `Set Variable`[/+]Snooz82 commentedon Nov 19, 2020
This could also just be an addition to BuiltIn library.
I am totally with you that
Set Variableis a bit unhandy.But a Keyword with the name
=or:=in BuiltIn would maybe just do the trick.That would maybe just easier to understand and the rules and an logs could stay the same.
If it is added to BuiltIn it is like a language feature.
pekkaklarck commentedon Nov 24, 2020
I agree creating local variables using a hard coded value is a bit annoying and coming up with something nice than
would be nice.
Is @mkorpela your idea that
would be a single token i.e. just one space around
:=? If yes, that would have a problem that it couldn't be lexed using the current general parsing rules and e.g. LSP needed to handle it separately. It wouldn't be compatible with how execution is logged either so it would require quite a lot of changes. I'd also use just=instead of:=(with required spaces around) because Python uses the "walrus operator" for assignment expression syntax and it behaves a bit differently.The idea to use
by @xylix is actually already mostly supported by the newish
but obviously this is a bit more verbose syntax. I still don't think adding yet another keyword would help and using something as generic as
Setwould also likely conflict with many keywords users have in their libraries.The approach proposed by @Snooz82 to allow using
=as a keyword name likeis actually something I've been thinking about earlier. It would have a great benefit that we just needed to make
=an alias forSet Variable(easy!) and then everything else ought to work just fine. Well, probably in some string representations and in the log file we needed to make sure that the assignment would look like${var} = valueand not like${var} = = value(i.e. the=automatically added with assignments shouldn't be added). A related problem would be that you could then in the data havebut I don't consider that a big problem. You just don't need to use such stupid syntax. Another problem is that
would look pretty similar but in the first case
xxxis a keyword and in the latter case it's a value to assign directly. I don't think that's a big problem either.If others agree, I'd be fine going with the above syntax and consider adding it already in RF 4. There would still require some design decisions related to how to handle lists and dicts but if that gets complicated just supporting the scalar assignment would already help a lot.
pekkaklarck commentedon Mar 3, 2021
This won't be in RF 4.0 but let's see could it be added in RF 4.1. Still time to think about the syntax as well.
[-]Shortcut syntax `:=` for `Set Variable`[/-][+]Native syntax to create local variables[/+]pekkaklarck commentedon Jun 24, 2021
Too big change for a small release like RF 4.1. Moving to RF 5.0.
boakley commentedon Jun 24, 2021
Would it make sense to make
:=an alias forSet Variable?MoreFamed commentedon Aug 26, 2021
@boakley, it sounds great to me! The confusion among '
:=' and that existing optional '=' would be lower than in case of "keyword="; and I think the same for case of equality comparison operator '=='. I came across opinion that simple '=' used for assigning is confusing, since people know equal sign as a comparison operator:${a} = 5evoke "a is equal to 5" more than "set a to 5". (I think this is the reason Pascal language, aimed at beginners in programming, used ':=' for assigning variables as well.)pekkaklarck commentedon Jan 17, 2022
No consensus on the syntax to use. Moving to RF 5.1 to give us more time to think about this.
We have also been thinking about inline keyword call syntax (#3187) and it should be taken into account when designing this syntax. These two features are interestingly related. A problem with variable assignment syntax is that in
${x} = somethingsyntax thatsomethingis a keyword and we'd like it to be a literal value. With the inline keyword call syntax the problem is opposite: insomething valuethatvalueis an argument and we wanted it to be a keyword.52 remaining items
[-]Native `VAR` syntax to create variables insinde tests/tasks and keywords[/-][+]Native `VAR` syntax to create variables inside tests and keywords[/+]Fine-tune control stucture option parsing.
pekkaklarck commentedon Oct 23, 2023
The
VARsyntax has been fine-tuned:=after the variable name the same way as when creating variables based on keyword return values and in the Variables section. This was requested by users on our Slack and implemented in 3625814.{'scope': 'value'}when earlierscopewas considered to be an option twice which caused an error.pekkaklarck commentedon Oct 24, 2023
We had a long discussion about the conditional
VARsyntax on our Slack. We had a vote about syntax alternatives I explained in an earlier comment and thevariant was generally preferred.
Things got more complicated when I realized that this kind of conditional assignment could be useful also in the Variables section. In that usage
ELSE IFsupport would likely be useful, but the above syntax cannot really be extended to handle it. It could thus be that thevariant that would also support
ELSE IF(see the aforementioned comment for an example) could be a better syntax choice.pekkaklarck commentedon Oct 24, 2023
In the end it turned out that it is best to post-pone adding the conditional assignment support to some future release. This is why:
VARsyntax is useful already in its current form.IF, including inlineIF, to set variables conditionally.ELSE IFvariant would be challenging to implement.VARsupport.After this decision the only remaining task related to the
VARsyntax in RF 7.0 ought to be documentation. Once that's ready, I think we are ready for an alpha release that makes it easier for users to test this new syntax.Document VAR syntax. #3761
Test VAR in suite setup and teardown.
pekkaklarck commentedon Oct 24, 2023
Documentation is now done and this issue ought to be ready. Looking forward for feedback once the alpha is out! At this point of the RF 7 development we can still make adjustments to the syntax if needed.
Recommend VAR over Set Variable keywords. #3761
MoreFamed commentedon Oct 26, 2023
I would strongly vote for the second syntax. There are two reasons for this:
Set Variable IfandRun Keyword Ifhas similar structure (i.e. there always comes the condition before the appropriate value);ELSE IFsupport for conditional settings of variables instead ofSet Variable If.Add more information about VAR to listener v2 API.