This post is part of the New Functionality In Microsoft Dynamics 365 Business Central 2024 Wave 2 series in which I am taking a look at the new functionality introduced in Microsoft Dynamics 365 Business Central 2024 Wave 2.
The 9th of the new functionality in the Development section is type testing and casting operators for interfaces.
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, Microsoft 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.
Enabled for: Admins, makers, marketers, or analysts, automatically
Public Preview: Oct 2024
General Availability: Oct 2024
Feature Details
In this version, Microsoft 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:
You can also use the is
operator with variants:
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:
Similarly, the as
keyword works with variants:
Click to show/hide the New Functionality In Microsoft Dynamics 365 Business Central 2024 Wave 2 Series Index
What should we write about next?
If there is a topic which fits the typical ones of this site, which you would like to see me write about, please use the form, below, to submit your idea.