3

I tried if(string? myStr) but that gives a syntax error in the editor. How do I do a type check in Ballerina ?

nuwanbando
  • 601
  • 4
  • 5

4 Answers4

2

You can use the type guard statement for that,

Eg:

    if (myStr is string) {
       io:println("This is String");
    } else {
       io:println("This isn't String");
    }
Nadeeshaan
  • 356
  • 5
  • 15
1

What is your use case for checking type?

As per the spec types and values is determined automatically based on their structure

Ballerina’s type system is much more flexible than traditional statically typed languages. First, it is structural: instead of requiring the program to explicitly say which types are compatible with each other, compatibility of types and values is determined automatically based on their structure; this is particularly useful in integration scenarios that combine data from multiple, independently-designed systems. Second, it provides union types: a choice of two or more types. Third, it provides open records: records that can contain fields in addition to those explicitly named in its type definition. This flexibility allows it also to be used as a schema for the data that is exchanged in distributed applications. Ballerina’s data types are designed to work particularly well with JSON; any JSON value has a direct, natural representation as a Ballerina value. Ballerina also provides support for XML and relational data.

Samisa
  • 1,012
  • 1
  • 10
  • 19
1

You can use type switch (match) statement or the expression version of the match statement here. Here is an example.

import ballerina/io;

function main (string... args) {
    any a = "some string value";

    // If the type of the variable a is string executes the first block, if not the second block.
    match a {
        string s => { io:println("string type");} 
        any k => {io:println("any other type");}
    }
}

Please refer the following example for more information. https://ballerina.io/learn/by-example/match.html

Sameera Jayasoma
  • 1,545
  • 8
  • 12
  • 1
    This example doesn't compile with Ballerina 1.0. I assume this feature is pre-1.0 and doesn't exists in Ballerina 1.0. Ballerina 1.0 implements _type test expression_ instead. – user272735 Oct 28 '19 at 14:26
1

I'm sure this is likely a sub-optimal answer (i.e. not covering all the aspects) but I have found out that in Ballerina 1.0 (that implements language specification 2019R3) one can use type test expression.

The best documentation I have found is:

Here's a working Ballerina 1.0 example with union and any types:

import ballerina/io;

public function main() {
    // union type
    int|error a = 0;
    io:print("typeof a: ");
    io:println(typeof a); // typeof expression

    // type test expression
    if (a is int) {
        io:println("a is int");
    } else {
        io:println("a is error");
    }

    // any type
    any b = "string";
    io:print("typeof b: ");
    io:println(typeof b); // typeof expression

    // type test expression
    if (b is int) {
        io:println("b is int");
    } else if (b is string) {
        io:println("b is string");
    } else {
        io:println("b is something else");
    }
}

Prints when run:

$ ballerina run test.bal 
typeof a: typedesc int
a is int
typeof b: typedesc string
b is string
user272735
  • 10,473
  • 9
  • 65
  • 96