I'm working with Ballerina and attempting to compare the types of two variables to ensure they have the same type. The code snippet looks like this:
typedesc type1 = typeof input1;
typedesc type2 = typeof input2;
if type1 == type2 {
//...
}
The == operator for type checks doesn't work as expected. I can use type1.toString() == type2.toString() but it does not work with union types and immutable records.
The
typedescequality can be checked using exact (reference) equality operator (===/!==) as follows. But, this and thetoString()approach won't work with immutable values.Also, types of the variables can be checked separately using the
isoperator. As a workaround, the following method can be used for the comparison of types. However, if there are two similar immutable type variables (except for the simple types) which have different values for the fields, the types can be unequal.