@@ -1455,9 +1455,8 @@ Sizes are determined dynamically (at run time) and thus cannot be
1455
1455
type-checked statically when the program is compiled. As a result,
1456
1456
any conformance error on size will raise a run-time error. For
1457
1457
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$.
1461
1460
1462
1461
### Type information excludes constraints {-}
1463
1462
@@ -1533,13 +1532,19 @@ a vector of length `N` (the previously declared variable), and a
1533
1532
variable ` A ` , which is a length-5 array where each element is a 3 by 4
1534
1533
matrix.
1535
1534
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.
1543
1548
1544
1549
``` stan
1545
1550
// valid block variables, but not locals or function parameters
@@ -1552,6 +1557,11 @@ array[3] int is;
1552
1557
void pretty_print_tri_lower(matrix x) { ... }
1553
1558
```
1554
1559
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.
1555
1565
1556
1566
In the following table, the leftmost column is a list of the
1557
1567
unconstrained and undimensioned basic types; these are used as
0 commit comments