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

The contractual behavior of interfaces has been a limiting factor with regards to update and future extensibility. Adding casting will greatly increase the usefulness of interfaces in AL. In this version, we introduce support for type testing and casting interfaces in the AL language. Two new operators, 'is' and 'as', have been added to facilitate these operations. The 'is' keyword checks if an interface is of a specific type, which is useful for ensuring type safety within code. The 'as' keyword, on the other hand, attempts to cast an interface to another interface. These operators improve the extensibility and usefulness of interfaces in AL and align with the broader programming practice of ensuring that systems are built with future growth and adaptability in mind, allowing for seamless updates and maintenance.

Feature details

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

The is operator

The is operator allows you to 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

The as operator is used for casting an instance of an interface to a specific interface. If the source interface doesn't implement the target interface, it will throw an error at runtime. Here’s an example:

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

Similarly, the as keyword works with variants:

Example of casting a variant to a specific interface.

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.

See also

Type testing and casting operators for interfaces (docs)