Skip to content

Idea: Accessing aspects of arrays of records directly #970

Closed

Description

@zayenz

Using records makes it clearer how data is structured in a model. However, in combination with many constraint being defined over arrays of aspects, it is easy to get into situations where there is a lot of boiler-plate. I think it might make sense to add the ability to access aspects of a record on an array of records.

As an example, consider this simple packing model using the no-overlap ("diffn") constraint

include "globals.mzn";


enum Boxes = B(1..10);
set of int: Pos = 1..100;

array[Boxes] of int: box_size = [20 | b in Boxes];

array[Boxes] of record(var Pos: x, var Pos: y): box;

constraint forall(b in Boxes) (
    box[b].x + box_size[b] in Pos /\
    box[b].y + box_size[b] in Pos
);

constraint diffn(array1d(Boxes, [box[b].x | b in Boxes]),
                 array1d(Boxes, [box[b].y | b in Boxes]),
                 box_size, 
                 box_size);

solve satisfy;

There is a lot of noise around writing the diffn-call. If we instead allowed box.x to represent a Boxes indexed array of var Pos, the constraint could be written like this

constraint diffn(box.x, box.y,
                 box_size, box_size);

With #968, the call would be less noisy, but having to do an array comprehension each time is still just busy-work.

Activity

  1. guidotack commented on Nov 19, 2025

    @guidotack
    Member

    This is pretty high on the to-do list (and should be easy to do). You don't even need #968, because box.x will have the same index set as box, so no additional coercion will be required.

  2. nicola-gigante commented on Nov 28, 2025

    @nicola-gigante

    Hi all,

    this would really simplify my life a lot!

    I see there's a commit doing that, do you think it will be merged in a release soon?

  3. added
    resolvedIssue is resolved and the feature or fix will be part of next release
    on Jan 14, 2026
  4. added theissue type on Jan 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    resolvedIssue is resolved and the feature or fix will be part of next release

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions