BCQuality rule · interfacesAL Developmentofficial - Microsoftderived - from the source, no model text

Guard optional interface casts with `is`

From runtime 15.0, AL can type-test an interface or Variant with is and cast it to another interface with as. The test is non-throwing, but as raises a runtime error when the underlying codeunit does not implement the target interface.

Rule on GitHub · microsoft layer · interfaces · BC 25.. · tier official · derived

Description

From runtime 15.0, AL can type-test an interface or Variant with is and cast it to another interface with as. The test is non-throwing, but as raises a runtime error when the underlying codeunit does not implement the target interface. This matters when an extended capability is optional or implementations can come from other extensions.

Best Practice

Use is to establish that the value supports the target interface before using as. Cast directly only where the target implementation is an invariant guaranteed by the surrounding contract.

See sample: guard-interface-casts-with-is.good.al.

Anti Pattern

Using as unconditionally for an optional extended interface. An otherwise valid implementation of the base interface then fails at runtime merely because it does not implement the additional contract.

See sample: guard-interface-casts-with-is.bad.al.

Reference

Understand type testing and casting operators for interfaces

Examples

Changed by


From microsoft/BCQuality at a4593a1, MIT License, Copyright (c) 2026 Microsoft Corporation (license).