zeek-docs icon indicating copy to clipboard operation
zeek-docs copied to clipboard

Gotcha for &ordered attribute on record fields

Open awelzel opened this issue 1 year ago • 2 comments

Talking with @bbannier a bit around, copy(), attributes, and gotchas. The following is a valid script today:

type R: record {
        s: set[string] &ordered &default=set("1", "2", "3", "4", "5");
};

event zeek_init() {
        print R();
}

And it probably doesn't do what was intended:

$ zeek x.zeek
[s={
4,
3,
1,
2,
5
}]

Putting &ordered as part of (or just behind) &default does likely what was intended:

type R: record {
        s: set[string] &default=set("1", "2", "3", "4", "5") &ordered;
};

We may consider this just a variation of the existing gotchas around attributes and how they bind to values rather than vars/fields. And possibly default initialization for containers isn't prevalent enough (though minimally Files::Info has analyzers with &default=string_set() - possibly historically), but maybe warning/erring on such constructs would not a bad idea.

awelzel avatar Feb 17 '23 14:02 awelzel