Back to Examples
- Binding patterns
- Typed binding pattern
- Wildcard binding pattern
- List binding patterns
- Rest binding pattern in list binding pattern
- Mapping binding pattern
- Rest binding pattern in mapping binding pattern
- Error binding pattern
- Rest binding pattern in error binding pattern
- Single use of typed binding patterns
- Single use of typed binding patterns with on fail clause
- Iterative use of typed binding patterns
- List binding pattern in match statement
- Mapping binding pattern in match statement
- Error binding pattern in match statement
- Query expressions
- Sort iterable objects
- Let clause
- Limit clause
- Join iterable objects
- Outer Join clause
- Query tables
- Create tables with a query
- Create maps with a query
- Create streams with a query
- On conflict clause
- Advanced conflict handling
- Iterate over XML with a query
- Nested query expressions
- Destructure records using a query
- Querying streams
- Aggregation
- JSON type
- Access JSON elements
- Access optional JSON elements
- Match statement with maps
- Convert from user-defined type to JSON
- Convert from table and XML to JSON
- Convert from JSON to user-defined type
- Cast JSON to user-defined type
- Resource method typing
- JSON numbers
- JSON to record
- JSON to record with projection
- JSONPath expressions
- Asynchronous function calls
- Named workers
- Sequence diagrams
- Wait for workers
- Strands
- Named worker return values
- Alternate wait
- Multiple wait
- Named workers and futures
- Inter-worker message passing
- Alternate receive
- Multiple receive
- Conditional send
- Inter-worker failure propagation
- Named worker with on fail clause
- Synchronize message passing
- Asynchronize message passing
- Flush
- Fork
Langlib functions
Langlib is a library defined by the language providing fundamental operations on built-in data types. Langlib functions can be called using the method-call syntax but these types are not objects.
There exists a ballerina/lang.T
module for each built-in type T
and they are automatically imported using the T
prefix only if T
corresponds to a keyword.
The lang.value
module provides functions that work on values of more than one basic type.
import ballerina/io;
public function main() returns error? {
// You can call langlib functions using the method-call syntax.
string str = "abc".substring(1, 2);
// `len` will be 1.
int len = str.length();
io:println(len);
// `str.length()` is same as `string:length(str)`.
len = string:length(str);
io:println(len);
int val = 123;
// The `lang.value` module provides functions that work on values of more than one basic type.
// `val.toString()` performs a direct conversion of `val` to string.
io:println("value is " + val.toString());
// `val.ensureType()` safely casts a value to a type and
// returns an error if the cast is impossible.
float|error floatVal = val.ensureType(float);
io:println(floatVal);
int[] evenNumbers = [2, 4, 6 ,8, 10, 12];
// `value.clone()` returns a clone of a value.
int[] clonedEvenNumbers = [2, 4, 6 ,8, 10, 12].clone();
// Following statement is `true`.
io:println(evenNumbers == clonedEvenNumbers);
// Following statement is `false`.
io:println(evenNumbers === clonedEvenNumbers);
// `value.cloneReadOnly()` returns a clone of a value that is read-only.
int[] & readonly immutableEvenNumbers = evenNumbers.cloneReadOnly();
io:println(immutableEvenNumbers);
// `value.cloneWithType()` constructs a value with a specified type by cloning another value.
float clonedVal = check val.cloneWithType(float);
// Following statement is `true`.
io:println(clonedVal);
}
$ bal run langlib_functions.bal11value is 123123.0truefalse[2,4,6,8,10,12]123.0
PreviousRange function
NextStructural typing