Skip to content

Native VAR syntax to create variables inside tests and keywords #3761

Closed

Description

@mkorpela

Robot Framework row is parsed by the following rules:

  • First column is thought as a keyword definition.
  • Unless if first column is a variable then second column is thought as a keyword definition.
  • Unless if the second column is also a variable then apply second rule again.
  • All the rest columns are paramenters to that keyword and by default they are strings (that can be converted to types).

Those rules result in an odd way of setting a simple value for a local variable:
${my_variable}= Set Variable MY_VALUE

I propose we introduce a shortcut for this to reduce unnecessary verbosity:
${my_variable}:= MY_VALUE

Activity

  1. xylix commented on Nov 15, 2020

    @xylix
    Contributor

    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.

  2. changed the title [-]Short cut syntax `:=` for `Set Variable`[/-] [+]Shortcut syntax `:=` for `Set Variable`[/+] on Nov 16, 2020
  3. Snooz82 commented on Nov 19, 2020

    @Snooz82
    Member

    This could also just be an addition to BuiltIn library.

    I am totally with you that Set Variable is a bit unhandy.
    But a Keyword with the name = or := in BuiltIn would maybe just do the trick.

    Test
        ${var}    =    Value
        ${list}    =     one    two    three
        ${dict}    =    key=val    key2=val2
        ${eval}     =    ${{sys.argv[1]}}
    

    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.

  4. pekkaklarck commented on Nov 24, 2020

    @pekkaklarck
    Member

    I agree creating local variables using a hard coded value is a bit annoying and coming up with something nice than

    ${var} =    Set Variable    value
    

    would be nice.


    Is @mkorpela your idea that

    ${var} := value
    

    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.

  5. pekkaklarck commented on Mar 3, 2021

    @pekkaklarck
    Member

    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.

  6. added this to the v4.1 milestone on Mar 3, 2021
  7. changed the title [-]Shortcut syntax `:=` for `Set Variable`[/-] [+]Native syntax to create local variables[/+] on Mar 3, 2021
  8. pekkaklarck commented on Jun 24, 2021

    @pekkaklarck
    Member

    Too big change for a small release like RF 4.1. Moving to RF 5.0.

  9. modified the milestones: v4.1, v5.0 on Jun 24, 2021
  10. boakley commented on Jun 24, 2021

    @boakley

    Would it make sense to make := an alias for Set Variable?

    *** Test cases ***
    Example
        ${message}  :=  Hello, world
        ${this}  ${that}  :=  string one  string two
    
  11. MoreFamed commented on Aug 26, 2021

    @MoreFamed

    @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.)

  12. pekkaklarck commented on Jan 17, 2022

    @pekkaklarck
    Member

    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.

  13. 52 remaining items

  14. 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
  15. pekkaklarck commented on Oct 23, 2023

    @pekkaklarck
    Member

    The VAR syntax has been fine-tuned:

    1. 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.
    2. 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.
  16. pekkaklarck commented on Oct 24, 2023

    @pekkaklarck
    Member

    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.

  17. pekkaklarck commented on Oct 24, 2023

    @pekkaklarck
    Member

    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.

  18. pekkaklarck commented on Oct 24, 2023

    @pekkaklarck
    Member

    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.

  19. MoreFamed commented on Oct 26, 2023

    @MoreFamed

    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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions