Post

Nil Literal

The nil basic type has only a single value called nil. Nil literal uses the syntax () and null.

✍ Syntax

Nil Literal

() | null

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import ballerina/io;

public function main() {

    () a = ();
    () b = null;

    io:println("a:", a);
    io:println("b:", b);
}
a:
b:
 bal version
Ballerina 2201.6.0 (Swan Lake Update 6)
// Partial Code. Change View  to see full code.
public function main() {
    () a = ();
    () b = null;
}
// Highlights Only. Change View  to see full code.
() a = ();
() b = null;

Code Breakdown

  • Line 5: nil literal () is assigned to the variable a.
  • Line 6: nil literal null is assigned to the variable b.

💡 Note: Both () and null are equivalent to nil in Ballerina. However, () is the preferred way to assign nil value to a variable.

 January 1, 2023

Table of Content

Key Words

Nil Literal
 January 1, 2023

Table of Content

Key Words


Navigation

Site Settings

Site Theme

Source Code Density