-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Procedures to return values #578
Comments
I was having a look at one of @AmritKumar ' s contracts today and it looks like this can be a useful thing. Checking permissions (is sender owner, does sender have permission to do this operation) etc involve common code, which ultimately return a bool (can do or cannot do operation), and their source values are from fields. So we would want procedures which look at certain fields, compute something on it, and return a value. |
If it's non-map fields, then it is just as simple to read the fields and compute the bool using a library function. The issue is if the field is a map. Then using library functions becomes expensive. |
Map fields are involved in this particular example, yes. |
Here's an example check:
This sequence appears in many transitions, and should be replaceable with
|
In the example contracts where this pattern occurs one of the conditional branches does nothing. Therefore, one could use a
If the checks fail, then execution stops when the exception is thrown, and the result of the transition is that no state is changed. If the check succeeds, then the calling transition continues as if everything is fine, which it is. |
Assigning @jubnzv to this. Suggested breakdown of the issue:
But this is a suggestion off the top of my head, so do put some thought into whether there might be a better option. At this stage we also need to introduce a typecheck of procedure invocations that don't expect a return value:
|
For now I suggest we add a typecheck in step 1 that disallows iteration of procedures that have a return value. We can then enable it for procedures with return values later on, once we decide how it should behave. |
This way of returning a value isn't great, because of this situation:
We'll need some sort of Maybe we need a special variable
? |
@jjcnn I checked the dump of all deployed contracts by 2022-07 and there are no uses of the I agree, that the |
This PR introduces the `Return` statement in the AST present with the `_return :=` construction in concrete syntax: ``` procedure foo() -> String a = my_string; _return := a; end ``` Design decisions in the current implementation: * `_return` takes as an argument a single variable with the return value. It seems natural to use this approach in the ANF-based language. * Don't support empty return statements (`_return`) for early exit when the procedure has no a return type. e.g. the following code is forbidden: ``` procedure no_return() match something with | 0 => (* do something *) | _ => _return; (* typechecking error: cannot use an empty return for early exit *) end end ``` Notes: * Gas cost of the return call is 1. * We use cram tests to check the typechecker because of #1196 Closes #578
Procedures are currently not allowed to return values.
I don't know how much of an issue this is, but some users may well find it useful to be able to pass a value back to the caller once a procedure finishes.
The text was updated successfully, but these errors were encountered: