import ballerina/io;function main (string[] args) {
any variable;
variable = 5;
typedesc typeOfVariable = (typeof variable);
typedesc intType = (typeof int);
if (typeOfVariable == intType) {
io:println("This 'variable' is an integer typed variable.");
} else {
io:println("This 'variable' is 'NOT' an integer typed variable.");
}
}
TypeofBallerina supports 'typeof' which returns the type of a variable. |
|
import ballerina/io;
|
|
function main (string[] args) {
|
|
any variable;
|
Here you create ‘any’ typed variable. |
variable = 5;
|
Assign an integer variable to ‘any’ typed variable. |
typedesc typeOfVariable = (typeof variable);
|
Use ‘typeof’ unary operator to return type of variable. |
typedesc intType = (typeof int);
|
Use ‘typeof’ unary operator to return type from type name. |
if (typeOfVariable == intType) {
io:println("This 'variable' is an integer typed variable.");
} else {
io:println("This 'variable' is 'NOT' an integer typed variable.");
}
}
|
Check for runtime type equivalency of ‘any’ typed variable. |
$ ballerina run typeof.bal
This 'variable' is an integer typed variable.
|
|