ballerina-lang icon indicating copy to clipboard operation
ballerina-lang copied to clipboard

Type Narrowing doesnt work with match Statement

Open gimantha opened this issue 3 years ago • 3 comments

Consider the following code

import ballerina/io;

type UnaryExpr record {|
    string operand;
|};

type TypeCastExpr record {|
    string oprd;
|};

type Expr UnaryExpr|TypeCastExpr;


function foo(Expr expr) {
    match expr {
        var {oprd: o} => {
            TypeCastExpr c = expr;
            io:println(c);
        }
    }
}

public function main() {
    Expr x = {oprd : "DFD"};
    foo(x);
}

This code gives the following error.

incompatible types: expected 'TypeCastExpr', found 'Expr'

gimantha avatar Jul 19 '21 09:07 gimantha