Error on missing side-effects

21 vistas
Ir al primer mensaje no leído

Sebastian Sardina

no leída,
12 feb 2021, 4:21:12 p.m.12/2/2021
para sarl
Hi

Why does the compiler give such error for this code?

========================
@Pure
    def agent_says(message : String, params : Object*) {
        info_with_agent(message, params)       
    }

    private def info_with_agent(message : String, params : Object*) {

        // Extend params with the agent name (last argument)
        var params2 : List<Object> = new ArrayList<Object>(params.size + 1)
        params2.addAll(params)
        params2.add(agentName)

        info(String.format("%s", message), params2.toArray())
    }
========================

Any use of agent_says will throw:

This expression is not allowed in this context, since it doesn't cause any side effects.


If I remove the @Pure from it, all good but then it complains if I give occurrence.XXX as one of the arguments.

Any explanation? What side effect should agent_says have?

Thanks

Sebastian

Sebastian Sardina

no leída,
12 feb 2021, 4:33:39 p.m.12/2/2021
para sarl
OK a bit more info..

agent_says() is part of a capacity/skill.

When @Pure is only given in the skill implementation, no error given BUT the usual warning on "Possible invalid usage of occurance", so it really does not realize it is using the @Pure implementation, fair enough as that would only be at compile time.

When I add @Pure in the capacity definition of agent_says() then I get the side-effect error at compile time.

Stéphane Galland

no leída,
13 feb 2021, 5:24:13 a.m.13/2/2021
para sarl
Dear Sebastian.

There is two sub problems in the one your have submitted.

First, a pure function has no border effect (no output, no changes of attributes, no changes of its arguments).
So a pure function is supposed to reply a value, otherwise it is a non sense to define a pure function that does nothing :-)
Consequently, you cannot call a pure function as a procedure. The value returned by the pure function must be stored/used.

This is explaining the "This expression is not allowed in this context, since it doesn't cause any side effects." problem.

Second, according to your code, your function should not be pure (call to info that is not pure). Then, the second problem may occur, that is more a warning and a notification of something to take care.
SARL compiler tries to infer if the uses of "occurrence" event may cause an change of the values stored in the occurrence event, because a received event is assumed to be read-only according to the SARL specifications.
In your case, the SARL "occurrence.XXX" is passed as argument of a not-pure function. The SARL compiler cannot analyze the content of the invoked function yet to determine if the value replies by occurrence.XXX is changed in the function (it is a future feature of the compiler). Then, the SARL compiler notifies you with a warning that you have to ensure by yourself that the attributes of the occurrence event are not changed.

In your case you could simply ignore the warning because you're sure that the value of "occurrence.XXX" is never changed.

Cheers.

Sebastian Sardina

no leída,
13 feb 2021, 7:54:29 p.m.13/2/2021
para sarl
Thanks Stephane,

Perfect. I keep confusing the same issue over and over. Good, so Pure means truly pure, no side-effect. I am always fixating on the second issue when SARL tries to figure out whether you will/may modify the event via occurrence.

So you are right, it is NOT a pure function, and this is the perfect case where I should ignore the warning.

Thanks!
Responder a todos
Responder al autor
Reenviar
0 mensajes nuevos