sarl: Expression with side effect is not allowed in guards

Describe the bug A clear and concise description of what the bug is.

Unecessary side-effect issue on behavior guards

To Reproduce

on E_MoveRandomly [occurrence.fromMe && (MT_getEntityState(occurrence.entity).routeLength > 0)] {

Expected behavior A clear and concise description of what you expected to happen.

Should work with no error

Screenshot image

System configuration: – SARL version: 0.10.0 – SARL compiler: 0.10.0

    • Eclipse compiler without Maven
    • Eclipse compiler with Maven
    • Maven compiler on the command line
    • sarlc compiler – Java JDK version (with the manufacturer name): 1.8 Oracle – Operating System:
    • Linux 64bits
    • Windows 64bits
    • MacOS 64bits

Additional context Add any other context about the problem here.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (15 by maintainers)

Commits related to this issue

Most upvoted comments

I will change the tables into the documentation. Because the “code” column provides the code for @SuppressWarnings. But, it is incomplete.

Dear @ssardina, regarding your comment about the list of all the errors that could be given by SARL, I have added this page for SARL, and this page for Janus. Both of them are accessible from several points into the documentation pages.

If you found that some explanaitions may be more detailed, please let us know.

According to the commit above, I confirm that the SARL compiler is providing the error “Side effect not allowed in guards” as expected.

The cause of the error is discussed above (not-standard function naming convention), and could be manually solved by attaching explicitly the @Pure annotation to the MT_getEntityState function.

I have moved this issue from “Bug” to “FAQ” and “Documentation” tags in order to upgrade the documentation sources with some information on this case.

Thank you for the code example. I think a bug may still exist that avoid SARL to properly detect the purity of the expression. We will use your inputs to set up a complete test.

Dear @ssardina.

The enforcement of no-side-effect in guards was introduced in version 0.9: guard expression must not have side effect. The problem is that your function MT_getEntityState is not detected as a pure (i.e. without side-effect) function.

A quick fix is, but far to be perfect:

on E_MoveRandomly {
    if (occurrence.fromMe && (MT_getEntityState(occurrence.entity).routeLength > 0)) {

A better fix is to mark the function MT_getEntityState as pure explicitly:

@Pure
def MT_getEntityState(E_MoveRandomly) : int {

Indeed, the purity of a function is detected according to a simple algorithm within the SARL compiler, so that a function pure when one of the following conditions becomes true:

  1. The function is marked with @Pure
  2. The function name follows one of the regular expression pattern: "clone", "contains(?:[A-Z1-9_][a-zA-Z1-9_]*)?", "equals", "get(?:[A-Z1-9_][a-zA-Z1-9_]*)?", "has[A-Z1-9_][a-zA-Z1-9_]*", "hashCode", "is[A-Z1-9_][a-zA-Z1-9_]*", "iterator", "length", "to[A-Z1-9_][a-zA-Z1-9_]*", "size".
  3. The code of the function contains no call to a side-effect (not pure) feature (this last test may be not perfect because not all Java function have been propertyl marked with @Pure).

As you could see, your naming convention for the function avoid the SARL compiler to assume that your function is pure, and your function’s code seems to call an not-pure function. In this case, explicit marking with @Pure is mandatory.