Swan Lake Beta2 is here.
import ballerina/io;

// Here type `int?` indicates that the value of `v` can be an `int` or `()`.
int? v = ();

// Here the value of `n` cannot be `()`.
int? n = v == () ? 0 : v;

// Elvis operator `x ?: y` returns `x` if it is not `nil` and `y` otherwise.
int m = v ?: 0;

// Falling off the end of a function or `return` by itself is equivalent to `return ()`.
function foo() returns () {
    return ();
}

// Leaving off return type is equivalent to `returns ()`.
public function main() {

    io:println(v);
}

Nil

Ballerina’s version of null is called nil and written as (). Ballerina types do not implicitly allow nil. Type T? means T or nil. You can use == and != to test whether a value is nil: no implicit conversion to boolean.

import ballerina/io;
int? v = ();

Here type int? indicates that the value of v can be an int or ().

int? n = v == () ? 0 : v;

Here the value of n cannot be ().

int m = v ?: 0;

Elvis operator x ?: y returns x if it is not nil and y otherwise.

function foo() returns () {
    return ();
}

Falling off the end of a function or return by itself is equivalent to return ().

public function main() {

Leaving off return type is equivalent to returns ().

    io:println(v);
}
bal run nil.bal
import ballerina/io;

// Here type `int?` indicates that the value of `v` can be an `int` or `()`.
int? v = ();

// Here the value of `n` cannot be `()`.
int? n = v == () ? 0 : v;

// Elvis operator `x ?: y` returns `x` if it is not `nil` and `y` otherwise.
int m = v ?: 0;

// Falling off the end of a function or `return` by itself is equivalent to `return ()`.
function foo() returns () {
    return ();
}

// Leaving off return type is equivalent to `returns ()`.
public function main() {

    io:println(v);
}

Nil

Ballerina’s version of null is called nil and written as (). Ballerina types do not implicitly allow nil. Type T? means T or nil. You can use == and != to test whether a value is nil: no implicit conversion to boolean.

import ballerina/io;
int? v = ();

Here type int? indicates that the value of v can be an int or ().

int? n = v == () ? 0 : v;

Here the value of n cannot be ().

int m = v ?: 0;

Elvis operator x ?: y returns x if it is not nil and y otherwise.

function foo() returns () {
    return ();
}

Falling off the end of a function or return by itself is equivalent to return ().

public function main() {

Leaving off return type is equivalent to returns ().

    io:println(v);
}
bal run nil.bal

In the creation of Ballerina, we were inspired by so many technologies. Thank you to all that have come before us (and forgive us if we missed one): Java, Go, C, C++, D, Rust, Haskell, Kotlin, Dart, TypeScript, JavaScript, Python, Perl, Flow, Swift, Elm, RelaxNG, NPM, Crates, Maven, Gradle, Kubernetes, Docker, Envoy, Markdown, GitHub and WSO2.

Cookie Policy

This website uses cookies so that we can provide you with the best user experience. Read our Cookie Policy to find out more.

If you wish to disable cookies you can do so from your browser.

I Understand