# nested &defaults

**URL:** <https://community.zeek.org/t/nested-defaults/2617>\
**Category:** Zeek\
**Created:** [March 25, 2013, 5:51pm UTC](https://community.zeek.org/t/nested-defaults/2617 "2013-03-25T17:51:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Mandelberg1](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@David\_Mandelberg1](https://community.zeek.org/u/David_Mandelberg1)\
**Post date:** [March 25, 2013, 5:51pm UTC](https://community.zeek.org/t/nested-defaults/2617/1 "2013-03-25T17:51:03Z")

</div>

Hello again,

I have a record with a field like this:

type Histogram: record {  
&nbsp;&nbsp;histogram: vector of count;  
};

I'd like histogram field to default to an empty vector and the  
individual counts in the vector to default to 0. I.e., I want the below  
code to print "42".

global foo: Histogram;  
foo$histogram[5] += 42;  
print(foo$histogram[5]);

Is that possible to set defaults for both $histogram and $histogram[N]?

---

<div class="post-metadata">

**Author:** ![Siwek\_Jon](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Siwek\_Jon](https://community.zeek.org/u/Siwek_Jon)\
**Post date:** [March 25, 2013, 6:32pm UTC](https://community.zeek.org/t/nested-defaults/2617/2 "2013-03-25T18:32:54Z")

</div>

I didn't think vectors supported a &default attribute very well, so I didn't try too hard to make that work, but here's what I came up with using a table indexed by counts and yielding counts (which is pretty much equivalent to a vector of count):

&nbsp;&nbsp;&nbsp;&nbsp;type HistogramType: table[count] of count;

&nbsp;&nbsp;&nbsp;&nbsp;function histogram\_default(index: count): count  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return 0;  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

&nbsp;&nbsp;&nbsp;&nbsp;const new\_histogram: HistogramType = table() &default=histogram\_default;

&nbsp;&nbsp;&nbsp;&nbsp;type HistogramRecord: record {  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;histogram: HistogramType &default=copy(new\_histogram);  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;other\_stuff: string &optional;  
&nbsp;&nbsp;&nbsp;&nbsp;};

&nbsp;&nbsp;&nbsp;&nbsp;global foo: HistogramRecord;  
&nbsp;&nbsp;&nbsp;&nbsp;global foo2: HistogramRecord;

&nbsp;&nbsp;&nbsp;&nbsp;foo$histogram[5] += 42;  
&nbsp;&nbsp;&nbsp;&nbsp;print(foo$histogram[5]);  
&nbsp;&nbsp;&nbsp;&nbsp;foo2$histogram[5] += 13;  
&nbsp;&nbsp;&nbsp;&nbsp;print(foo2$histogram[5]);

The big trick there is that attributes don't currently propagate from types to values, so instead of setting a &default function on the HistogramType, I had to apply it to a constant, empty table and clone that for every new histogram instance.

- Jon

---

<div class="post-metadata">

**Author:** ![David\_Mandelberg1](https://avatars.discourse-cdn.com/v4/letter/d/958977/32.png) [@David\_Mandelberg1](https://community.zeek.org/u/David_Mandelberg1)\
**Post date:** [March 25, 2013, 7:19pm UTC](https://community.zeek.org/t/nested-defaults/2617/3 "2013-03-25T19:19:08Z")

</div>

Thanks, that works! One question below, though:

---

<div class="post-metadata">

**Author:** ![Siwek\_Jon](https://avatars.discourse-cdn.com/v4/letter/s/90db22/32.png) [@Siwek\_Jon](https://community.zeek.org/u/Siwek_Jon)\
**Post date:** [March 25, 2013, 7:39pm UTC](https://community.zeek.org/t/nested-defaults/2617/4 "2013-03-25T19:39:23Z")

</div>

> Why not just do &default=0 instead of histogram\_default? That works for  
> me.

I was only playing around to verify what I could get to work. Since you don't need the default value to be a function of the index, &default=0 is fine.

- Jon

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/zeek/original/1X/f09d732bc2cc7c7cc7e35db67cf4e1d5233ce7a7.png) [@system](https://community.zeek.org/u/system)\
**Post date:** [May 6, 2022, 3:40pm UTC](https://community.zeek.org/t/nested-defaults/2617/5 "2022-05-06T15:40:52Z")

</div>

Closed on May 6, 2022, 3:40pm UTC


