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
Optional fields
Fields of a record
type can be marked as optional. These fields can be omitted when creating a value of the record type. A field f
of record type r
can be accessed via optional field access (e.g., r?.f
) or member access (e.g., r["f"]
), which will both return ()
if the field is not present in the record value.
A record that contains optional fields can be destructured. If the optional field is not available, the type of the variable becomes nil
. The optional field value can also be removed from the record by assigning ()
to the optional field.
import ballerina/io;
type FullName record {
string firstName;
string lastName;
// The `title` and `middleName` fields are optional.
string title?;
string middleName?;
};
public function main() {
FullName name = {
title: "Mr",
firstName: "John",
lastName: "Doe"
};
// Use the `?.` operator to access the optional field.
io:println("Title: ", name?.title);
// A variable of type `string?` is used to construct an optional field.
string? middleName = name["middleName"];
io:println("Middle name: ", middleName);
// Remove the optional `title` field by assigning `()`.
name.title = ();
io:println(name.hasKey("title"));
// When destructuring the record `name`, if the `title` field is absent,
// then, its value becomes nil.
var {title, firstName: _, lastName: _} = name;
io:println(title is ());
}
$ bal run optional_fields.balTitle: MrMiddle name: falsetrue
Related links
PreviousComputed field key
NextOpen records