RFC-049: FOREACH with `as` Binds Only Its Name

ProposedImplemented
Depends onRequired by

Decision

A FOREACH with as binds one name in its filter and body: the as. The element is reachable as $<as> and its fields as $<as>.<field>. A field is no longer bound under its own name.

An explicit as: item counts as as: its fields are read as $item.<field>.

A FOREACH without as keeps the rule of RFC-016: the element is $item, and the fields of an object element are also bound under their own names.

operation: FOREACH collection: $verlofaanvragen as: aanvraag filter: operation: EQUALS subject: $aanvraag.bsn_jongere # the field of the row value: $bsn_jongere # the parameter, not the field body: $aanvraag.verlofdagen combine: ADD

Context

RFC-016 binds every field of an object element under its own name, with or without as. A field then shadows an outer variable of the same name. A reduction with a parameter bsn_jongere, over rows that also have bsn_jongere, cannot compare the row with the parameter: inside the body both names are the field. The only way out was a helper output under another name.

RFC-016 already advised dot notation for new laws for this reason. With as the law has named the element, so the bare field names add nothing and only take names away.

Consequences

  • The engine binds the fields as bare names only when as is absent.
  • The type checker (RFC-037) treats a bare name in the body of a FOREACH without as as possibly a field, and claims nothing about it. With as, a bare name is the declared variable.
  • The dependency closure (RFC-043) and the enrichment check on undefined names follow the same rule. With as, an undefined bare name in the body is a finding.
  • The corpus needed no rewrite: no FOREACH with as read a field by its bare name.
  • The schema does not change. Its description of as (“Defaults to ‘item’”) gets the clarification that an explicit as: item binds only $item in the next schema release.

Alternatives Considered

  • Drop the bare field names everywhere. Breaks the laws that read fields bare in a FOREACH without as, such as the demo corpus’s wet_structuur_uitvoeringsorganisatie_werk_en_inkomen.
  • Let the outer variable win over a field. Then whether $status is a field or an input depends on what the law declares, and adding an input changes what a body reads.

RegelRecht

An exploration into transparent, executable legislation, and one of the three projects in the starting selection of the Nederlandse Digitale Dienst.

Links

GitHub repository
How it works
Stay informed
Roadmap (Dutch)
Documentation
Research

Contact

regelrecht@minbzk.nl

Part of

Nederlandse Digitale Dienst
Ministry of Economic Affairs and Climate Policy