Swan Lake Beta2 is here.
import ballerina/io;

type Coord record {
    float x;
    float y;
};

public function main() returns error? {
    json j = {x: 1.0, y: 2.0};

    // Argument is a `typedesc` value.
    // The static return type depends on the argument.
    Coord c = check j.cloneWithType(Coord);

    io:println(c.x);

    // Argument defaulted from the context.
    Coord d = check j.cloneWithType();

    io:println(d.x);
}

Converting to User-Defined Type

The cloneWithType langlib function in the lang.value module can be used to convert a value to a user-defined type. Result recursively uses specified type as inherent type of new value. Automatically performs numeric conversions as necessary. Every part of value is cloned, including immutable structural values. Graph structure is not preserved. Variant fromJsonWithType also does reverse of conversions done by toJson.

import ballerina/io;
type Coord record {
    float x;
    float y;
};
public function main() returns error? {
    json j = {x: 1.0, y: 2.0};
    Coord c = check j.cloneWithType(Coord);

Argument is a typedesc value. The static return type depends on the argument.

    io:println(c.x);
    Coord d = check j.cloneWithType();

Argument defaulted from the context.

    io:println(d.x);
}
bal run converting_to_user_defined_type.bal
1.0
1.0
import ballerina/io;

type Coord record {
    float x;
    float y;
};

public function main() returns error? {
    json j = {x: 1.0, y: 2.0};

    // Argument is a `typedesc` value.
    // The static return type depends on the argument.
    Coord c = check j.cloneWithType(Coord);

    io:println(c.x);

    // Argument defaulted from the context.
    Coord d = check j.cloneWithType();

    io:println(d.x);
}

Converting to User-Defined Type

The cloneWithType langlib function in the lang.value module can be used to convert a value to a user-defined type. Result recursively uses specified type as inherent type of new value. Automatically performs numeric conversions as necessary. Every part of value is cloned, including immutable structural values. Graph structure is not preserved. Variant fromJsonWithType also does reverse of conversions done by toJson.

import ballerina/io;
type Coord record {
    float x;
    float y;
};
public function main() returns error? {
    json j = {x: 1.0, y: 2.0};
    Coord c = check j.cloneWithType(Coord);

Argument is a typedesc value. The static return type depends on the argument.

    io:println(c.x);
    Coord d = check j.cloneWithType();

Argument defaulted from the context.

    io:println(d.x);
}
bal run converting_to_user_defined_type.bal
1.0
1.0

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