-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
v: add $res compile time function to get returned value in defer block #18382
v: add $res compile time function to get returned value in defer block #18382
Conversation
I can see the utility of something like that, but I am afraid, that it would prevent our To me, having a Potentially, we could have both defer kinds, if there is a syntax for distinguishing the 2 kinds of defer (function scoped defer, and lexical scope defer), but that is a decision that @medvednikov should make. |
Unrelated to my other comment, this new feature does need documentation in |
@spytheman we're most likely going to have 2 defers: one for the end of the block, and one for the end of the function. |
I can see the use of a But in your case the code can be simplified by changing the control flow a bit. pub fn (mut web Web) check_auth() bool {
auth_header := web.get_header('Authorization').split(' ')
eprintln(auth_header)
if auth_header.len == 2 && auth_header[0] == 'Bearer' && auth_header[1] == 'test' {
return true
} else {
web.set_status(401, 'Unauthorized')
web.ok('')
return false
}
} Because C evaluates expressions lazely and we first verify that |
I see what you mean @spytheman. But now, since there will be 2 different defer statements, what happens with this merge request, because the feature isn't a huge mess of code; it could be merged (and obviously I would add docs and changelog) or the feature will come at another time, when the differentiation of the defer statements is done (which is a bit too much for me, while I don't have that much time for it). |
…-defer # Conflicts: # vlib/v/fmt/fmt.v
I still don't fully understand the need, does any other language have something similar? |
In go there is a feature, which uses named return values (https://riandyrn.medium.com/golang-magic-modify-return-value-using-deferred-function-ed0eabdaa75), which also allows modifying the value in the defer statement, as well as use it directly; e.g. func test1() (i int) {
defer func() { i++ }()
return 1
} |
The utility is if you need to quickly/conveniently do some processing/logging of the result value of a function, without having to refactor the existing code first (that used multiple return lines, potentially with different expressions each), to use a |
@LouisSchmieder given that @medvednikov agreed that there will be 2 variants of defer (i.e. that a scoped defer will be possible in the future), I do not have objections to merging that on technical grounds. The feature just needs docs. |
This PR add the feature to get the returned value of the function inside a defer block. This PR only adds this feature for functions, which return one or more values; Results are not included.
I added this feature, since it can be very helpful in a situation like this:
It can be simplified to this:
And when the complexity of this function increases, it gets even more helpful, since there is no additional variables handling for the result.
🤖 Generated by Copilot at 39bbc24
This pull request adds support for the
res
function, which allows accessing the returned value of a function inside a defer block. It modifies the parser, checker, formatter, and generator modules to handle theres
function in defer statements and comptime calls. It also adds several test files to verify the correct behavior and error handling of theres
function.🤖 Generated by Copilot at 39bbc24
res
that allows accessing the returned value of a function inside a defer block (link, link, link, link, link, link)fn_return_type
to theChecker
struct to store the return type of the current function being checked (link)fn_return_type
field of the checker in theChecker.check_fn_decl
method (link)defer_return_tmp_var
to theGen
struct to store the name of the temporary variable that holds the returned value of a function (link)defer_return_tmp_var
field of the generator in theGen.gen_fn_decl
method (link)res
function call (link)res
function usage and error cases in thevlib/v/checker/tests
andvlib/v/tests
directories (link, link, link, link, link, link, link, link, link, link, link)