Return values ​​and assertions from functions

I am curious about the difference in behavior between using a statement return

and defining a function with a templatefoo() = Expression

It is my understanding from Funtions that a function body can be = Expression

and return

accepts either nothing orExpression

The reason I am asking is because, in my understanding of the documentation, the following should not work:

map[int,int] foo(map[int,int] env) = env[0] = 1;

      

Which it doesn't do (causing a parsing error) since the assignment is Statement

. However, if I modify this function a bit:

map[int,int] foo(map[int,int] env) { return env[0] = 1; }

      

It seems to work. I am confused because although I return

have to accept expressions. Also, it looks like when playing with the REPL this assignment returns Value

, so it would be nice to define functions that assign and return the modified map.

Maybe I missed something.

+3


source to share


1 answer


Your understanding is fine. The return statement has some additional flexibility, although it allows, accordingly, to return the value produced by the selected types of statements, such as "for" and assignment. See the definition of the syntax of the return statement in the Rascal grammar here



In future releases, we may remove all restrictions and allow statements everywhere.

+1


source







All Articles