Defer Statement in V Language

Written by on April 20, 2023 in Programming, Vlang with 0 Comments

The defer statement in Vlang causes code to be executed when the enclosing function returns. This feature is inspired by Golang, but is slightly more flexible since it allows any block of code to be associated with defer instead of just a function call.

defer is used primarily to handle “clean up” logic, such as closing files, etc. For those who are familiar with C++, RAII is the common idiom to implement acquisition of resources in constructors and releasing them in destructors. In Vlang where such a luxury is not available, defer statement comes in handy for freeing resources. Of course, any logic can go into the defer block.

Let us look at some examples to understand how defer can be used.

The "defer" Statement

The “defer” Statement

In the above example, the defer block contains two statements, one to write some text into the trace file and the other to close the file. Even though this block appears “lexically” in the beginning of the function, it will be executed only when the function “returns”. In other words, it will be executed either after line 19, or after line 23, depending on the actual path taken. In this case, the argument to the function determines the actual path taken. Let us see this in action.

Here is the main function:

The Main Function

The Main Function

Here we are calling the function by passing “false” as argument. As a result, the “if” condition succeeds and the function returns after line 19. At this point, our defer block will be executed.

The Output

The Output

A look at the trace file shows that the defer block is executed after the “if” block code.
Let us call the same function now, but passing “true” this time:

Passing "true" as Argument

Passing “true” as Argument

Here is the corresponding trace:

Program Output

Program Output

This time, the “if” block is bypassed and line 23 is executed, followed by defer block.

This example demonstrates how the defer statement can be used to implement “clean up” logic just before function return.

Multiple “defer” Statements

Is it possible to define multiple defer statements inside a function? The answer is “yes”. Take a look at the following code:

Multiple "defer" Statements

Multiple “defer” Statements

In the above example, we have two defer statements inside the function. In addition, the function returns a value to the caller. When multiple defer statements are present, these statements will be executed in the “reverse” order and the returned value will be made available to the caller (we are not using the returned value). Here is the “main” function:

Calling from Main

Calling from Main

And here is the output:

Program Output

Program Output

An important point should be mentioned here. If you look at line 33, you can see that I have commented the “return” statement. Why? Simply because it doesn’t make sense to have a “return” statement inside a defer block. Since, by definition, the defer block is called just before returning from the function, allowing a “return” inside the block will cause infinite loop!

The Impact of “panic”

We all know that returning from a function is the normal function call behavior. And we have seen how the defer statement is guaranteed to be executed “just before” the function returns normally. What if there is an abnormal situation during program execution? In V language, the “panic” function is used to signal an “abnormal” situation. In this case, the program terminates abruptly, perhaps printing a stack trace, etc. Will the defer block be executed in such a situation? The answer is No. The following function demonstrates this.

Calling "panic"

Calling “panic”

Depending on the argument passed to the function, the function will return normally, or exit through “panic”.
Let us see what happens when we pass “false”, therefore bypassing the “if” block:

The main Function

The “main” Function

Here is the output:

Output from Program

Output from Program

The defer block is executed as expected. Let us now pass “true”, triggering a panic:

Passing "true" This Time

Passing “true” This Time

This is what happens in this case:

Abnormal Termination

Abnormal Termination

We can see that the defer block is not executed.

It is important to stress that the defer statement is executed when a function “returns normally” and not when there is an abnormal exit. Nevertheless, it gives us a convenient point to define our “resource clean up” code.

You can download the source file here. I have used V 0.3.3 for testing my code.

Have a nice week!

Tags: , ,


If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *