Share via


Type testing and casting operators for interfaces

Enabled for Public preview General availability
Admins, makers, marketers, or analysts, automatically Oct 1, 2024 Oct 1, 2024

Business value

Contractual behavior of interfaces limits updates and future extensibility. Adding casting greatly increases the usefulness of interfaces in AL. In this version, the AL language supports type testing and casting interfaces. Two new operators, is and as, facilitate these operations. The is keyword checks if an interface is a specific type. This check is useful for ensuring type safety within code. The as keyword attempts to cast an interface to another interface. These operators improve the extensibility and usefulness of interfaces in AL. They align with the broader programming practice of building systems for future growth and adaptability. With these operators, you can update and maintain your code seamlessly.

Feature details

In this version, we introduce support for casting between AL interfaces. Given an interface A, you can cast it to interface B if the underlying object (only codeunit objects for now) implements interface B. To make these operations easier, we added two new operators: is and as.

The is operator

The is operator lets you test whether an instance of an interface or the content of a variant supports a specific interface. Here's the syntax for using the is keyword:

Example of testing if a given instance of an interface supports a specific interface

You can also use the is operator with variants:

Example of testing if a given variant supports a specific interface

The as operator

Use the as operator to cast an instance of an interface to a specific interface. If the source interface doesn't implement the target interface, the operation throws an error at runtime. Here's an example:

Example of casting an instance of an interface to a specific interface.

The as keyword works with variants too:

Example of casting a variant to a specific interface.

Geographic areas

Visit the Explore Feature Geography report for Microsoft Azure areas where this feature is planned or available.

Language availability

Visit the Explore Feature Language report for information on this feature's availability.

Tell us what you think

Help us improve Dynamics 365 Business Central by discussing ideas, providing suggestions, and giving feedback. Use the forum at https://aka.ms/bcideas.

Additional resources

Type testing and casting operators for interfaces (docs)