import ballerina/io;function main (string[] args) {
any a = "Jungle cat";
string str = <string>a;
io:println(str);
}
Type CastingType casting allows you to look at a value as being of a different type than it’s original type. There are safe and unsafe casts. For unsafe casts, Ballerina compiler will enforce you to use multi-return cast expression. |
|
import ballerina/io;
|
|
function main (string[] args) {
|
|
any a = "Jungle cat";
|
Here we assign a ‘string’ type value to a variable of type ‘any’. |
string str = <string>a;
io:println(str);
}
|
Here is how you can cast an ‘any’ type variable to the ‘string’ type. This cast is unsafe, because the value of the variable ‘a’ is unknown till runtime. Therefore the compiler will enforce you to use multi-return type cast expression. This is an error that occurs during a Type Casting. |
$ ballerina run type-casting.bal
Jungle cat
|
|