bcquality/microsoft/knowledge/interfaces/set-defaultimplementation-on-enum.md
Jesper Schulz-Wedde 484ee120af Add interfaces knowledge domain and review leaf skill
Adds the interfaces knowledge domain covering AL interfaces and enum-with-implementation: three atomic articles with good/bad AL samples, a new al-interfaces-review leaf skill, and additive wiring into al-code-review and the README. Purely additive; no contract change.

Part of #34.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
2026-06-23 11:46:39 +02:00

2.2 KiB

bc-version domain keywords technologies countries application-area
16..
interfaces
interface
defaultimplementation
enum-implements-interface
fallback
extensible-enum
implementation-property
al
w1
all

Set DefaultImplementation on an enum so an unmapped value still resolves to an interface

Description

An enum that implements an interface maps each value to a codeunit through the Implementation property. But an extensible enum can carry values that set no Implementation — values added later by an extension, or a value left intentionally blank. Assigning such a value to an interface variable and calling a method on it fails at runtime unless the enum provides a fallback. The enum-level DefaultImplementation property names the codeunit used whenever a value has no explicit Implementation, so resolution always yields a usable object. LLMs are generally unaware this property exists and leave the gap open.

Best Practice

On any extensible enum that implements an interface, set DefaultImplementation = <Interface> = <Codeunit>; at the enum level, pointing at a safe implementation that does nothing harmful. Values with their own Implementation keep using it; every other value — including ones added later by extensions — resolves to the default instead of failing. For the distinct case of an out-of-range integer that matches no declared value, pair it with UnknownValueImplementation. The result is that a consumer can assign any enum value to the interface variable and call through it without a runtime guard.

See sample: set-defaultimplementation-on-enum.good.al.

Anti Pattern

An extensible enum ... implements <Interface> where at least one value sets no Implementation and the enum declares no DefaultImplementation. Code that assigns that value to an interface variable and invokes a method throws at the call site, and because the enum is extensible the failing value can be introduced by a third party long after the consumer ships. Detection signal: an enum that implements an interface, has a value(...) with no Implementation, and no enum-level DefaultImplementation. Add a DefaultImplementation mapping to close the gap.

See sample: set-defaultimplementation-on-enum.bad.al.