Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you get the type of a variable in Ballerina?

Tags:

wso2

ballerina

How do you get the type of a variable in Ballerina?

I know that boolean checks are possible such as these below:

import ballerina/io;

public function main() {
    any x = "This is a test";
    io:println(x is string); // Evaluates to true
    io:println(x is float); // Evaluates to false
}

In Python, we use type(variable) and get the type, in Java it is as follows:

String a = "test";
a.getClass().getName()

How do we do this in Ballerina? I've tried to look in the docs and the closest I can find is lang.typedesc.

like image 262
Rahul P Avatar asked Nov 17 '25 08:11

Rahul P


1 Answers

You can use the typeof expression for get the type of any variable in Ballerina.

import ballerina/io;

public function main() {
    var x = 5;
    io:println(typeof x);
}

Please refer to the "Typeof expression" section of the language specification below for more information.

https://ballerina.io/spec/lang/2019R3/#section_6.25

like image 152
Chanaka Lakmal Avatar answered Nov 19 '25 11:11

Chanaka Lakmal



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!