{{announcement.body}}
{{announcement.title}}

Defaultable Parameters in Ballerina

DZone 's Guide to

Defaultable Parameters in Ballerina

Let's look at Ballerina and see some examples of invocations, two-parameter functions, and more.

· Integration Zone ·
Free Resource

Ballerina

Ballerina

Let's dig into this cool Ballerina feature. How many times you were wondering how to invoke a method and what to pass into that function? Of course, you should know what to pass, but there are occasions that you care about only a couple of parameters and the rest of the parameters you just don't know what to do with.

Personally, I faced such situations multiple times. Ballerina provides a cool way to deal with such scenarios. Basically, the function developer can provide the default value for the parameter so that the one who uses the function and invokes it can opt-out from providing a value for that parameter.

You might also like:  Introduction to Ballerina: Language of Integration

Let's look at an example to understand this more. Below is a Ballerina function that has two string parameters, but the second parameter is an optional parameter.

public function encode(string content, string charset = "UTF-8") {

}


When invoking the method, you can simply choose to ignore that parameter altogether and override only when necessary. Example invocations would be as follows

public function main() {
    // Invoke with first parameter, second will get defaulted to "UTF-8"
    encode("example");

    // Invoke with both parameters 
    encode("example", "UTF-16");

    // Invoke as named parameters
    encode("example", charset = "UTF-16");

    // Invoke as named parameters
    encode(content = "example", charset = "UTF-16");

    // Invoke as named parameters
    encode(charset = "UTF-16", content = "example");

}


If you have noticed, we can use both styles of invocation where the argument to parameter mapping is deduced from the index of the argument in the invocation (the first argument to the first parameter and so on), as well as you can pass the arguments as named values. In the case of passing them as named values, the order doesn't matter. Cool, right? :)

So the next question is to what extent do we support this? Is it only simple literals you can use as default values? No, you can use any kind of expression as the default value. Below is an example where you invoke a function as the default value of a parameter.

import ballerina/io;

public function main() {
    // invoking without value
    invocationAsDefault();

    // invoking with value
    invocationAsDefault(abc(10));

}

public function invocationAsDefault(int asyncVal = abc(8)) {
    io:println(asyncVal);
}

public function abc(int a) returns int {
    return a + 5;
}


Another cool thing is when you compose these expressions, you can use values of previous parameters in those expressions. Below is a two-parameter function where the value of the first parameter is used to calculate the default value of the second parameter.

import ballerina/io;

public function main() {
    // Invoke without values
    paramExample(5);

    // Invoke with values
    paramExample(5, 20);
}

public function paramExample(int a, int b = a + abc(a)) {
    io:println(a + b);
}

public function abc(int a) returns int {
    return a + 5;
}


This behavior is similar across the board (that is even extern and interop functions also can have default parameters).

As you may know, Ballerina works in a non-blocking manner, which is an internal thing, but in effect, it means Ballerina won't block threads unnecessarily. I mentioned that here because what I wanted to emphasize that you can even do non-blocking invocations as parameter default values.

Note: This post was written for the Ballerina version 1.0.0. 
Ballerina site: https://v1-0.Ballerina.io

Further Reading

Design-First With Ballerina

Unpacking Ballerina Non-Blocking Architecture

Topics:
ballerina ,programming languages ,coding ,integration language ,integration ,invoke a method

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}