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

Unnecessary calls to `TypeChecker.checkCast` for compile time know subtypes

Open manuranga opened this issue 4 years ago • 1 comments
trafficstars

Consider the following code.

const OFF = 0;
type Color "red" | "green" | "blue" | OFF;

function parseColor(string c) returns Color {
   if c is "r" {
       return "red";
   }
   else if c is "g" {
       return "green";
   }
   else if c is "b" {
       return "blue";
   }
   else if c is "0" {
       return OFF;
   }
   panic error("unexpected input : " + c);
}


In this there is no need to check each given constant (eg "red") is a subtype of Color since it's known at compile time.

manuranga avatar Aug 04 '21 14:08 manuranga