# Constraining leaf elements of 2D Array of handle

**URL:** <https://verificationacademy.com/forums/t/constraining-leaf-elements-of-2d-array-of-handle/51743>\
**Category:** SystemVerilog\
**Created:** [September 23, 2026, 4:23pm UTC](https://verificationacademy.com/forums/t/constraining-leaf-elements-of-2d-array-of-handle/51743 "2026-09-23T16:23:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Have\_A\_Doubt](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/51bf81/32.png) [@Have\_A\_Doubt](https://verificationacademy.com/forums/u/Have_A_Doubt)\
**Post date:** [September 23, 2026, 4:23pm UTC](https://verificationacademy.com/forums/t/constraining-leaf-elements-of-2d-array-of-handle/51743/1 "2026-09-23T16:23:19Z")

</div>

Hi Moderators,

Based on discussion in [thread](https://verificationacademy.com/forums/t/understanding-reduction-operator-sum-on-class-handles/37115), I was trying sum() for 2-dimensional array of handle where I observe unexpected results.

Link: [edaplayground](https://edaplayground.com/x/DrvG)

Although the output in $display matches the constraint expression i.e **top.sum() is 'd10**,

**the output of $write differs i.e total no. of leaf elements with value of ON isn’t 10.**

My expectation is that **out of the 24 leaf elements of enum type, 10 would be ON**

Across different tools and multiple seeds I observe that no. of elements with value of ON is 0 or 4 or 8 or 12 or 16 **whereas I expected only 10**

The amusing thing is on replacing the sum() constraint with the equivalent expression mentioned in the multiline comments, I observe the same unexpected results.

I’d appreciate any insights or suggestions.

Thanks in advance

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [September 23, 2026, 4:42pm UTC](https://verificationacademy.com/forums/t/constraining-leaf-elements-of-2d-array-of-handle/51743/2 "2026-09-23T16:42:38Z")

</div>

You have a typo in your `$write` statement.

---

<div class="post-metadata">

**Author:** ![Have\_A\_Doubt](https://verificationacademy.com/forums/letter_avatar_proxy/v4/letter/h/51bf81/32.png) [@Have\_A\_Doubt](https://verificationacademy.com/forums/u/Have_A_Doubt)\
**Post date:** [September 23, 2026, 4:49pm UTC](https://verificationacademy.com/forums/t/constraining-leaf-elements-of-2d-array-of-handle/51743/3 "2026-09-23T16:49:36Z")

</div>

Thanks Dave.

The 3rd argument should be top[i].**b[j]**.bb ( previously I had written top[i].**b[i]**.bb )

A final question from my side

If I were to write constraint as

```verilog
top.sum() with ( item.b.sum with ( int'(item.bb) ) ) == 10;

```

**Why does the 2nd `item` refer to top[X].b[Y] whereas the 1st item refer to top[X] ?**

It seems that **item** automatically refers to nearest call to sum()

---

<div class="post-metadata">

**Author:** ![dave\_59](https://verificationacademy.com/forums/user_avatar/verificationacademy.com/dave_59/32/4499_2.png) [@dave\_59](https://verificationacademy.com/forums/u/dave_59)\
**Post date:** [September 23, 2026, 9:18pm UTC](https://verificationacademy.com/forums/t/constraining-leaf-elements-of-2d-array-of-handle/51743/4 "2026-09-23T21:18:42Z")

</div>

All local iterator variables work this way. If the iterator variables have the same name, the nearest loop variable takes precedence. This effectively hides the outer iterator variable. You should give them unique names.
