Any type support in ballerina

Rajith Vitharana
Ballerina Swan Lake Tech Blog
1 min readMay 4, 2017

--

Ballerina is the new era in integration world which you can get an idea through link [1]. That link has all the resources relevant to get up to date with this next generation integration language.

In this article I’m going to focus on a simple but powerful feature in this new language, “Any” type support.

With this feature you can simply assign any type of variable to this “any” type. The advantage is you don’t need to know about the type before hand to define the variable.

For example, below are valid statements with “any” type support.

any json = `{“key1” : “ value1”, “key2” : “value2”}`;

any intVal = 5;

any stringVal = “this is a sample string”;

This is also supported in function parameters and return types as well. So for example you can define a function as follows

function calculateSalary(any income, any tax) (any netSalary) {}

When invoking the function, you can pass any kind of value to those parameters.

However when working with “any” type, you need to cast the value to the actual type before doing anything with those values.

For this, there will be a .type in variables which you can use to identify actual type of that variable(but still it’s not fully merged to the code base). So for the time being, what we can do is just cast the value to the required type and ballerina will throw a runtime exception if the casting is wrong.

[1] — http://ballerinalang.org/

--

--