Swan Lake Beta3 is here.
int[] iv = [1, 2, 3];

// Assigning `int[]` to `any[]` is allowed.
// - set of values allowed by `int` is subset of set of values allowed by `any`
// - set of values allowed by `int[]` is subset of set of values allowed by `any[]`
any[] av = iv;

public function main() {
    // A runtime error or else `iv[0]` would have the wrong type.
    av[0] = "str";

}

Covariance

Arrays and maps are covariant. Static type-checking guarantees that the result of a read from a mutable structure will be consistent with the static type. Covariance means that a write to a mutable structure may result in a runtime error. Arrays, maps, and records have an “inherent” type that constrains mutation.

int[] iv = [1, 2, 3];
any[] av = iv;

Assigning int[] to any[] is allowed. - set of values allowed by int is subset of set of values allowed by any - set of values allowed by int[] is subset of set of values allowed by any[]

public function main() {
    av[0] = "str";

A runtime error or else iv[0] would have the wrong type.

}
bal run covariance.bal
error: {ballerina/lang.array}InherentTypeViolation {"message":"incompatible types: expected 'int', found 'string'"}
        at covariance:main(covariance.bal:10)
int[] iv = [1, 2, 3];

// Assigning `int[]` to `any[]` is allowed.
// - set of values allowed by `int` is subset of set of values allowed by `any`
// - set of values allowed by `int[]` is subset of set of values allowed by `any[]`
any[] av = iv;

public function main() {
    // A runtime error or else `iv[0]` would have the wrong type.
    av[0] = "str";

}

Covariance

Arrays and maps are covariant. Static type-checking guarantees that the result of a read from a mutable structure will be consistent with the static type. Covariance means that a write to a mutable structure may result in a runtime error. Arrays, maps, and records have an “inherent” type that constrains mutation.

int[] iv = [1, 2, 3];
any[] av = iv;

Assigning int[] to any[] is allowed. - set of values allowed by int is subset of set of values allowed by any - set of values allowed by int[] is subset of set of values allowed by any[]

public function main() {
    av[0] = "str";

A runtime error or else iv[0] would have the wrong type.

}
bal run covariance.bal
error: {ballerina/lang.array}InherentTypeViolation {"message":"incompatible types: expected 'int', found 'string'"}
        at covariance:main(covariance.bal:10)
Subscribe to the newsletter

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