Skip to content

Inline keyword call syntax #3187

Open

Description

@pekkaklarck

Having inline keyword call syntax would be nice. It would allow using keywords in places where currently you'd normally just have plain strings (or variables). Example usages are listed below and possible syntax alternatives discussed in separate comments.

  1. Arguments to other keywords. At the moment you need to this in two steps:

    ${result} =    Keyword    argument
    Another Keyword    ${result}
    

    In Python and other "real" programming languages you use something like another_keyword(keyword('argument')), but in Robot's syntax

    Another Keyword    Keyword    argument
    

    is parsed as Another Keyword getting two arguments. This cannot be changed so new syntax is needed.

  2. Condition to the forthcoming IF/ELSE syntax (Native support for IF/ELSE syntax #3074). Currently (well, in the future once IF/ELSE is supported), you need to use:

    ${status} =    Keyword
    IF    ${status}
        Another Keyword
    END
    

    and

    IF    Keyword
        Another Keyword
    END
    

    would be parsed as Keyword being the expression to evaluate.

  3. Similarly existing FOR loops would benefit from this.

  4. Inline keyword call syntax would also allow adding support to create variables in the Variables section based on keyword return values. Currently that's not possible at all, and creating suite level variables dynamically requires using Suite Setup. Being able to do something like this is a pretty common request:

    *** Variables ***
    ${EXAMPLE} =         Keyword    argument
    

The hardest thing related to this issue is coming up with a good syntax. I've been thinking about this for some time and have some ideas but no clear candidate I like best. I'll explain these ideas in separate comments below. 👍 and 👎 reactions, comments and other syntax ideas are all highly appreciated!

Activity

  1. added this to the v3.2 milestone on May 16, 2019
  2. pekkaklarck commented on May 16, 2019

    @pekkaklarck
    MemberAuthor

    My first idea was using [Keyword|argument] syntax with single spaces allowed like [ Keyword | argument ] as well. Examples:

    *** Variables ***
    ${EXAMPLE}        [ Keyword | argument ]
    
    *** Test Cases ***
    Example
        Another Keyword    [ Keyword | argument ]
        IF    [Keyword]
            Something
        END

    Pros:

    • [ | ] visually match Robot's tabular syntax and looks pretty good in general.
    • Relatively simple.

    Cons:

    • Same syntax that used with test/keyword level settings like [Documentation]. This probably wouldn't be a technical problem because inline keyword calls probably wouldn't be allowed in places where settings are allowed, but same syntax would still be confusing to users.
    • Not possible to expand return value to list or dictionary like @{list} and &{dict} variables allow. Most importantly this means that usage with FOR loops so that one keyword returns all values to iterate wouldn't be supported.
  3. pekkaklarck commented on May 16, 2019

    @pekkaklarck
    MemberAuthor

    Second syntax ideas is using ${[ Keyword | argument ]}. Example:

    *** Variables ***
    ${EXAMPLE}        ${[ Keyword | argument ]}
    
    *** Test Cases ***
    Example
        Another Keyword    ${[ Keyword | argument ]}
        IF    ${[Keyword]}
            Something
        END
        FOR    ${item}    IN    @{[ Keyword Returning Items ]}
            Log    ${item}
        END

    Pros:

    Cons:

    • More boilerplate. Ugly.
    • ${[xxx]}, ${{xxx}} and ${xxx} all look pretty similar.
  4. pekkaklarck commented on May 16, 2019

    @pekkaklarck
    MemberAuthor

    Yet another idea is using a variation of the current variable syntax $[ Keyword | argument ]. Example:

    *** Variables ***
    ${EXAMPLE}        $[ Keyword | argument ]
    
    *** Test Cases ***
    Example
        Another Keyword    $[ Keyword | argument ]
        IF    $[Keyword]
            Something
        END
        FOR    ${item}    IN    @[ Keyword Returning Items ]
            Log    ${item}
        END

    Pros:

    • Less boilerplate than in ${[ Keyword | argument ]} syntax.
    • Easier to separate from variables.
    • Supports list and dict expansion.

    Cons:

    • More boilerplate than in [ Keyword | arguments ].
    • $[xxx] and ${xxx} still look similar.
    • Cannot be implemented as a simple extension to the current variable syntax. More work.
  5. aaltat commented on May 16, 2019

    @aaltat
    Contributor

    For me $[ keyword | argument ] looks very close to the variable syntax. ${ is pretty same as $[. Although I have to admit that I don't have better idea and IDE can highlight the syntax better.

  6. pekkaklarck commented on May 17, 2019

    @pekkaklarck
    MemberAuthor

    It's true that $[Something] looks very much like ${Something} so the benefits of using that instead of ${[Something]} wouldn't be that big. If we'd use $[Something] with inline keyword calls, we could also consider using $(expression) with inline Python evaluation (#3179). But I'm not sure is ${var}, $(expr), $[kw] really better than ${var}, ${{expr}}, ${[kw]}.

  7. HelioGuilherme66 commented on May 17, 2019

    @HelioGuilherme66
    Member

    What if to be more clear that it is not a variable we double the []?

    $[[ Keyword | argument ]]
    
  8. sebastianciupinski commented on May 21, 2019

    @sebastianciupinski

    What about pipelines or redirecting? I know it may be problematic with keyword accepting varargs, but anyway (pipeline symbols, i.e. '<$' and '$>' are just an idea):

    ${result} =    Keyword      ${input}
    ${result2}=    Another Keyword    ${result}
    

    could be:

    ${result} =    Keyword      ${input}
    ${result2}=    Another Keyword    <$    Keyword    ${input}
    #or
    ${result2}=    Keyword    ${input}     $>     Another keyword
    

    But according to proposed approaches, I think following !{} which mean 'Evaluate what is inside'

    ${result2}=    Another keyword    !{Keyword      ${input}}
    
    

    or to distinguish what is returned:

    ${result2}=    Another keyword    !${Keyword      ${input}}
    
    
    ${result2}=    Another keyword    !@{Keyword returning array      ${input}}
    
    

    looks better than proposed:

    ${result2}=    Another keyword    [Keyword | ${input}]
    ${result2}=    Another keyword    ${[ Keyword | ${input}]}
    
    
    

    because using !{} does not mix two ways of separating keywords and arguments (one with tabulators, second with pipe symbol)

  9. rainydew commented on Jun 18, 2019

    @rainydew

    it that more natural?

    ${result}=    Another keyword     (      Keyword      ${input}      )
    

    parentheses may be better

    I use pycharm and IntelliBot, but it may be ugly in RIDE cell view

  10. boakley commented on Jun 27, 2019

    @boakley

    backticks would have a little bit of symmetry with some languages, as well as with markdown:

    ${result}=  Another keyword  `keyword  arg1  arg2`
    

    Perhaps the rule would be "any cell that begins with a backtick will be treated as an inline keyword invocation, up until a cell that ends with a backtick`" All parsing inside the backticks wouldn't change -- you would still use two spaces between cells.

  11. sebastianciupinski commented on Jun 27, 2019

    @sebastianciupinski
  12. modified the milestones: v3.2, v3.3 on Aug 26, 2019
  13. 36 remaining items

  14. modified the milestones: v5.1, v6.0 on Apr 1, 2022
  15. Nagesh389 commented on Jul 29, 2022

    @Nagesh389

    Hi Team,

    Im trying to Pass a keyword result as an argument to another keyword in the same line in Robot framework and getting error like Keyword expected 1 argument, got 2. Tried many ways as mentioned above but still getting the same issue.
    Can you help on this.

    Regards,
    Nagesh K

  16. JFoederer commented on Jul 29, 2022

    @JFoederer
    Contributor

    Hi @Nagesh389 ,
    The syntax mentioned in this issue, is not currently available in any Robot framework release yet. It is planned for a future release.

  17. Snooz82 commented on Sep 26, 2022

    @Snooz82
    Member

    @pekkaklarck i know that Lisp has a different approach. but i doubt that this approach would be a good one for our community.

    We have in my opinion started a path that now should be followed.

    ${scalar}, @{unpacke_list}, &{unpacked_dict} is a concept that is known by more advanced users.
    Beginners already struggle with that.

    Now introducing a totally new concept like lisp does, would just make it more complex and complicated to use.

    Square brackets [ ] is really not an unpacked list, but a closed one
    So using [ keyword] as unpacking the results does feel really strange to me.

  18. Snooz82 commented on Sep 26, 2022

    @Snooz82
    Member

    I still think that this would be the most readable and understandable syntax:

    Log    $((keyword | arg1 | arg2))
  19. JFoederer commented on Oct 23, 2022

    @JFoederer
    Contributor

    For the people who want to gain some experience in using inline keywords, before we decide on the final syntax. With this robotnl library you can now add inline keyword support to your libraries. Compared to the full version as discussed in this issue, there are two limitations. Evaluation is at runtime and inline keywords are limited to a single cell, occupying only the argument's position. But even with these limitations, for me, it works in most situations where I have the need for inline keywords. Feel free to give it a try.

  20. mkorpela commented on Oct 24, 2022

    @mkorpela
    Member

    @JFoederer I like what you have there :)
    There are (at least) 2 really nice ideas:

    1. Using ”kind of special type converters” to allow inline keyword calls
    2. The check method that has similarities to assertion engine in Browser lib
      Both are on their own sides solving the problem of verbosity in RF.
  21. stylpe commented on Mar 14, 2023

    @stylpe

    I have a proposal that I think is a reasonable balance between all the different constraints that have been discussed: !{Keyword} and possibly !{Keyword | arg}.

    • It is consistent with the existing RF syntax and conventions for variables and substitutions, without the downside of being hard to distinguish that the alternative brackets have or any extra whitespace. It should therefore also be easy to understand and adopt. I agree with the sentiment that the precedent has already been set for RF syntax and straying too far from it would create friction; you would basically need to reinvent the language to come out net positive.
    • The specific symbol ! was chosen to be somewhat intuitive and representative of its executive effect. It should also be largely backwards compatible; it doesn't seem likely to me that this character sequence would be used in tests already, and the risk can be mitigated with a command line feature flag. And it could of course be changed to any other symbol the maintainers would prefer.
    • I can picture combining it with @ and & to also unpack the return values, e.g. @!{Returns list} and &!{Returns dict}.
    • If I was the product owner here, I would start with just implementing !{Keyword}, and make the !{Keyword | arg} feature a separate milestone, to keep the scope and effort manageable and shippable.
    • It decisively meets the core need of a simple, short way to run a keyword and pass on the returned value without making an intermediate variable.
    • As for how to log it, I would suggest treating it as basically syntax sugar for normal variable assignment, so Keyword 1 !{Keyword 2} would be logged similarly to if you had written ${temp}= Keyword 2 \n Keyword 1 ${temp}

    I also have some general thoughts to share, independent from that proposal: Like @HelioGuilherme66 and others have said, the most important need to meet here is that of simplification. It's ok to have some limitations for inline keywords regardless of syntax particulars, similar to what for example the setup and teardown keywords already have and with the same recommendations of making dedicated keywords if you need more complicated expressions, though potentially with the added escape hatch of using pipes to supply arguments. The number one use case for this is going to be to slap in a quick short utility keyword without If you feel you need something more complex like nested keywords, you should instead try to keep readability in mind and either use intermediate variables as before, or make helper keywords, or better yet drop down to Python code, because that's where this belongs.

    Hope that helps! I'm hoping some version of this makes it into a release eventually 👍

  22. pekkaklarck commented on Aug 25, 2023

    @pekkaklarck
    MemberAuthor

    This is too big for RF 7 especially when we haven't yet agreed on the design.

  23. modified the milestones: v7.0, v8.0 on Aug 25, 2023
  24. sebastianciupinski commented on Dec 5, 2024

    @sebastianciupinski

    @stylpe

    reasonable balance between all the different constraints

    except similar approach has been proposed already: #3187 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions