import ballerina/io;
// A variable of type `any` can hold any value except an `error` value.
any x = 1;
public function main() {
// Can cast `any` to specific type.
int n = <int>x;
io:println(n);
// The `lang.value` lang library contains functions that apply to multiple basic types.
// `x.toString()` converts `x` to a `string`.
string s = x.toString();
io:println(s == "1");
// Can test its type with the `is` operator.
float f = x is int|float ? <float>x : 0.0;
io:println(f);
}
Any type
|
import ballerina/io;
any x = 1;
A variable of type any
can hold any value except an error
value.
public function main() {
int n = <int>x;
io:println(n);
Can cast any
to specific type.
string s = x.toString();
The lang.value
lang library contains functions that apply to multiple basic types.
x.toString()
converts x
to a string
.
io:println(s == "1");
float f = x is int|float ? <float>x : 0.0;
Can test its type with the is
operator.
io:println(f);
}
bal run any_type.bal
1
true
1.0