Arrays - CAST to HAC Migration Guide
Description: CAST to HAC Migration Guide

Next: Connections, Previous: Instances, Up: Top

## 5 Arrays

In CAST, the dimensions were declared between the type-identifier and the instance-identifier. Arrays in HAC are syntactically C-style, where the dimensions of the array follow the array's identifier.

Sparse arrays are also supported but with different syntax. In HAC, sparse arrays are always declared using a range expression in the indices. e.g. ``inv x[1..1];`' declares a sparse 1D array populated at index 1. (Yes, some of you may find this inconvenient.)

The following table shows some examples of equivalent declarations in CAST and HAC, where `inv` is defined as a type.

Examples of dense and sparse array declarations:

CAST | HAC | meaning
--- | --- | ---
`inv[2] x;` | `inv x[2];` | 1D array with indices 0..1
`inv[2] x, y;` | `inv x[2], y[2];` | 2 1D arrays with indices 0..1
`inv[2][3] x;` | `inv x[2][3];` | 2D array with indices 0..1,0..2
`inv x[2];` | `inv x[2..2];` | sparse 1D array indexed 2 only
`inv x[2..4];` | `inv x[2..4];` | sparse 1D array indexed 2..4
`inv x[2], x[4];` | `inv x[2..2], x[4..4];` | sparse 1D array indexed 2, 4 only

A common pitfall is to declare sparse index of an array and pass port connections in the same statement, such as: ``inv w[2](x, y, z);`'. This illegal statement tries to declare an array indexed 0..1, and connect both instances with the same port parameters. In HAC, one cannot declare a collection and connect its ports in the same statement as in CAST, however, one may declare a scalar instance and connect its ports in the same statement. The proper way to instantiate and connect a sparse instance is to use a sparse range, just like in a sparse declaration: ``inv w[2..2](x, y, z);`'.