Confusion on calling SV system task $size for dynamic types

Hi Moderators,

Section 5.13 of IEEE Std 1800-2023 mentions

In general, a built-in method is preferred over a system task when a particular 
functionality applies to all data types or when it applies to a specific data type.
For example: dynamic_array.size, associative_array.num, and string.len
These are all similar concepts, but they represent different things. 
A dynamic array has a size, an associative array contains a given number of
items, and a string has a given length. Using the same system task, such as 
$size, for all of them would be less clear and intuitive.

This section doesn’t explicitly say that using $size for dynamic type is illegal

Section 7.5.2 mentions

The size dynamic array method is equivalent to the $size(addr, 1) array query 
system function (see 20.7)

My interpretation is of this quote is that $size is legal for dynamic array.

Similarly it should be legal for queue as well

Later Section 20.7.1 says

int a[3][][5]; // array dimension 2 has variable size
$display( $unpacked_dimensions(a) ); // displays 3
a[2] = new[4];
a[2][2][0] = 220;        // OK, a[2][2] is a 5-element array
$display( $size(a, 1) ); // OK, displays 3
$display( $size(a, 2) ); // ERROR, dimension 2 is dynamic

[Q1] Why is $size(a,2) considered an error here ?

[Q2] Does LRM explicitly mention that $size() is illegal for dynamic array / queue type ?

Thanks

The main issue here is accessing the dimensions of a multidimensional array. SystemVerilog doesn’t inherently support these multiple dimensions everywhere; instead, it has arrays of any type, including arrays of arrays. When you try to find the size of the second dimension, each array within the first dimension might have a different size for its second dimension because it is dynamically sized. As a result, the $size method won’t work. You’ve only dynamically allocated element a[2] to 4, while the rest of the elements in the dimension still have zero elements.