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
Readonly and isolated
isolated
functions can access final
variables with readonly
type without locking. It relies on the fact that immutability is deep. isolated
for functions complements readonly
for data.
import ballerina/io;
type Entry map<json>;
type RoMap readonly & map<Entry>;
final RoMap m = loadMap();
function loadMap() returns RoMap {
readonly & Entry entry1 = {
"munich": {latitude: "48.1351N", longitude: "11.5820E"},
"berlin": {latitude: "52.5200N", longitude: "13.4050E"}
};
readonly & Entry entry2 = {
"bordeaux": {latitude: "44.8378N", longitude: "0.5792W"},
"paris": {latitude: "48.8566N", longitude: "2.3522E"}
};
RoMap roMap = {"germany": entry1, "france": entry2};
return roMap;
}
isolated function lookup(string s) returns readonly & Entry? {
// Accesses `m` directly without locking.
return m[s];
}
public function main() {
io:println(lookup("france"));
}
Executing the above code gives the output below.
$ bal run readonly_and_isolated.bal{"bordeaux":{"latitude":"44.8378N","longitude":"0.5792W"},"paris":{"latitude":"48.8566N","longitude":"2.3522E"}}
PreviousReadonly type
NextReadonly objects and classes