[Ballerina] Casting and Conversions — Part I

Maryam Ziyad
3 min readDec 11, 2018

--

In this post and the next, we will be looking into the latest changes introduced to casting and conversions in Ballerina, with the Ballerina 0.990.0 release, which introduced some major improvements to the language! Check out the release notes for detailed explanations on all new features and changes.

If you are not new to Ballerina, and have used a pre-0.990.0 version, you might be familiar with the diamond operator in Ballerina, which used to serve as either a cast or conversion operator depending on the usage.

With Ballerina 0.990.0 casting/conversion has changed quite a bit, and is all more explicit.

Let’s take a look at the operators/operations now available with a couple of examples:

The Diamond Operator

The diamond operator in Ballerina, now serves to specify a contextual type for an expression explicitly.

T1 val = <T1> exp;

In the most generic case, if the expression (exp) is not exactly of type T1, the evaluation of <T1> exp would result in panic.

Type Assertion

So, what the diamond operator essentially does is asserting an expression to be of a particular type.

This could be used to downcast the result of an expression to an expected type.

For example:

This would result in the following output:

$ ballerina run blog_type_assertion_one.bal
true
one

Observe how we’ve asserted the type to be of map<string> in L11.

If we change the asserted type to something like map<int> as follows:

map<int> strMapTwo = <map<int>> a;

the assertion at L11, would fail at run time with the following error:

error: {ballerina}TypeAssertionError {"message":"assertion error: expected 'map<int>', found 'map<string>'"}
at main(blog_type_assertion_one.bal:11)

Assertions pass only for exact types.

Even if an expression’s type is asserted to a super (assignable) type, the assertion would still fail.

Assertion looks at the inherent type.

Assume there are two records Employee and Person where Employee is assignable to Person.

The following examples demonstrate how assertion looks at inherent types:

This would print true as the output.

Now try changing the asserted type in L11 to Person (the type of the variable itself).

Note how the assertion fails at L11, despite the variable being of type Person, since the inherent type is Employee.

error: {ballerina}TypeAssertionError {"message":"assertion error: expected 'Person', found 'Employee'"}
at main(blog_type_assertion_three.bal:11)

Thus now, the diamond operator is used for type assertion/casting to inherent type, and not for conversions!

If your requirement is to do a type test instead (and execute conditional logic based on possible types), do check out the official Ballerina by Example on Type Guards, which discusses Ballerina’s type switch.

The only exception to this rule is with simple basic types.

Explicitly Typed Expressions

Ballerina has the simple basic types string, int, float, decimal, boolean and nil (). Using the diamond operator with combinations of non-nil basic types results in conversions, except when the source type is string and the target type is not string, in which case there would be a compilation error (i.e., string to non-string is not allowed with the diamond operator!).

Running the above would result in conversions and output the following:

$ ballerina run expl_typed_expr.bal
10
true
0.0

But what about conversions for non-basic types?

Now that we’ve understood the new role of the diamond operator, we know that we cannot use the diamond operator for conversion between non-basic types.

But what if I want to convert a non-basic type to another non-basic type or even a basic type.

What if I want to convert a string to a non-string basic type?

Do checkout the next post where we discuss how Ballerina allows these conversions now!

Sign up to discover human stories that deepen your understanding of the world.

--

--

No responses yet

Write a response