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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With