Saad Sahibjan

Ballerina Syntax - underscore variable

Note: Version used 0.962.0

Ballerina is a general purpose, concurrent and strongly typed programming language with both textual and graphical syntaxes, optimised for integration by WSO2. An underscore is a pre-defined variable of type any. This tech guide will be helpful to readers who wish to understand the ‘_(underscore)’ variable and its usage in WSO2 Ballerina.

Note: Version used 0.962.0

Ballerina is a general purpose, concurrent and strongly typed programming language with both textual and graphical syntaxes, optimised for integration by WSO2.

What is _ (underscore) variable?

An underscore is a pre-defined variable of type any. The value of the _ (underscore) variable will be an ignored value. Basically, it implies to ignore the returned arguments of multi-return functions.

In the below code snippets (code snippet 1 & 2), the function testUnderscore returns three values of types string, float and int. In the main function, testUnderscore function is used and both of the returned values (of types string and int) are assigned to two variables and the returned float value is ignored.

(Code Snippet 1)

(Code Snippet 2)

 

_ (underscore) variable with HTTP Client Connector

When a request is made with the use of HTTP client connector, it returns multiple values which means the functions (get, post, put, patch, delete) are multi return functions. These functions, return HTTP response object and HTTP connector error object. If the request is successful, the response details (response payload, status code, etc) will be as HTTP response object and the HTTP connector error object will be null.

If the request is not successful, the  HTTP connector error object will have the information (error message, cause for the error, etc) on error during HTTP client invocation and HTTP response object will be null.

The below code snippet explains the creation of an HTTP endpoint and making a GET request. And further, the HTTP connector error returned by the GET request is ignored by using the _ (underscore) variable.

(Code Snippet 3)

 

The above code works perfectly, but in case the HTTP connection was not successful, a null reference exception will be thrown due to invoking the getJsonPayload method using the resp variable which is null as the request was not successful.

Further, we will never know the reason behind the error during the request since we have ignored the connection error returned by using the _ (underscore) variable.

Thus, in such a scenario we should not ignore the connection error response. Instead, we recommend to handle the connection error response. In the below code snippet connection error response is handled instead of using the _ (underscore) variable to ignore.

(Code Snippet 4)

 

According to the above code, if the request is not successful (due to connection error) the reason for error is printed and if the request is success the response payload is printed in JSON format. In this case null reference exception will not be thrown.

We hope this post will be helpful to understand the ‘_ (underscore) variable’ and its uses in WSO2 Ballerina.

Thank you for reading our latest Mitra Innovation Tech Guide We hope you will also read our next tutorial so that we can help you solve some more interesting problems.

Saad

Saad Sahibjan

Software Engineer | Mitra Innovation