Skip to content

Commit ffecad9

Browse files
authored
Merge pull request #781 from stan-dev/issue/774-size-must-be-data-var
Issue/774 size must be data var
2 parents 419c978 + 1faf69d commit ffecad9

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/reference-manual/types.qmd

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,9 +1455,8 @@ Sizes are determined dynamically (at run time) and thus cannot be
14551455
type-checked statically when the program is compiled. As a result,
14561456
any conformance error on size will raise a run-time error. For
14571457
example, trying to assign an array of size 5 to an array of size 6
1458-
will cause a run-time error. Similarly, multiplying an $N
1459-
\times M$ by a $J \times K$ matrix will raise a run-time error if $M
1460-
\neq J$.
1458+
will cause a run-time error. Similarly, multiplying an $N \times M$
1459+
by a $J \times K$ matrix will raise a run-time error if $M \neq J$.
14611460

14621461
### Type information excludes constraints {-}
14631462

@@ -1533,13 +1532,19 @@ a vector of length `N` (the previously declared variable), and a
15331532
variable `A`, which is a length-5 array where each element is a 3 by 4
15341533
matrix.
15351534

1536-
There are several different places a variable is declared in Stan. They are
1537-
block variables, like those inside `data`, which can have
1538-
[constraints](#constrained-data-types) and must include sizes for their types,
1539-
like in the above examples. Local variables, like those defined inside loops
1540-
or local blocks cannot be constrained, but still include sizes. Finally,
1541-
variables declared as [function parameters](user-functions.qmd#argument-types-and-qualifiers)
1542-
are not constrained types and _exclude_ sizes.
1535+
The size of top-level variables in the `parameters`, `transformed parameters`, and `generated quantities`
1536+
must remain constant across all iterations, therefore only data variables can be used in top-level size declarations.
1537+
1538+
```stan
1539+
// illegal and will be flagged by the compiler:
1540+
generated quantities {
1541+
int N = 10;
1542+
array[N] int foo;
1543+
```
1544+
1545+
Depending on where the variable is declared in the Stan program,
1546+
it either must or cannot have size information, and constraints
1547+
are either optional or not allowed.
15431548

15441549
```stan
15451550
// valid block variables, but not locals or function parameters
@@ -1552,6 +1557,11 @@ array[3] int is;
15521557
void pretty_print_tri_lower(matrix x) { ... }
15531558
```
15541559

1560+
Top-level variables can have [constraints](#constrained-data-types) and
1561+
must include sizes for their types, as in the above examples.
1562+
Local variables, like those defined inside loops or local blocks cannot be constrained, but still include sizes.
1563+
Finally, variables declared as [function parameters](user-functions.qmd#argument-types-and-qualifiers)
1564+
are not constrained types and _exclude_ sizes.
15551565

15561566
In the following table, the leftmost column is a list of the
15571567
unconstrained and undimensioned basic types; these are used as

0 commit comments

Comments
 (0)