<p>Some languages require array types to be declared before an object of that type can be declared. In Ada, for example:</p>
<pre><code> type My_Int is range 0 .. 1000;
 type Index is range 11 .. 15;
 type My_Int_Array is array (Index) of My_Int;
 Tab : constant My_Int_Array := (2, 3, 5, 7, 11);
</code></pre>
<p>rather than simply</p>
<pre><code>Tab : constant array(11 .. 15) of integer(0 .. 1000) := (2, 3, 5, 7, 11);
</code></pre>
<p>What other languages have this restriction? Is there any particularly good reason to do this, apart from just making life easier for the compiler?</p>

<arrays><declarations>
Languages in which array types must be declared before usage
CC BY-SA 4.0
2024-05-14T10:59:47.130Z
Is this just about array types or any complex types in general? `constant array(11 .. 15) of integer(0 .. 1000)` is quite a mouthful, so simple readability could motivate a rule to forbid non-primitive types as part of larger expressions.
MisterMiyagi
CC BY-SA 4.0
2024-05-14T15:03:03.273Z
This sounds like it could be a special case of a more general rule, that type annotations can only refer to types by name. That suggests a language design reason, perhaps because allowing arbitrary syntax for types to appear in variable declarations might lead to an ambiguity if some of the same syntax might also be used for an expression.
kaya3
CC BY-SA 4.0
2024-05-14T18:55:37.390Z
The example given doesn't appear to be true, at least in current versions of Ada. Here's an online compiler happily accepting a fully inline array type: http://tpcg.io/_7AOR14 And I can't see any note in [this annotated reference manual](http://www.ada-auth.org/standards/22aarm/html/AA-3-6.html) that the syntax used to only allow a "subtype name" here, rather than any "subtype indication".
IMSoP
CC BY-SA 4.0
2024-05-15T08:39:02.970Z
@IMSoP: interesting; thanks. I don't write Ada, but I do write VHDL (which was too obscure to mention). VHDL does have this restriction, but was based on Ada 83. Maybe this was added in Ada 95+, or it was considered too 'difficult' for VHDL? I have no idea. I got the example from adacore.com's section on arrays.
QF0
CC BY-SA 4.0
2024-05-15T08:45:17.700Z
@MisterMiyagi: I suspect that, for VHDL (see other comment), compiler efficiency was more of a concern than readability. One of the original implementers is on record as saying that processing power in the early 80s was a factor in the language design.
QF0
CC BY-SA 4.0
2024-05-15T08:47:23.517Z
@QF0 I don't want to rule out efficiency as a reason, I'm just wondering if the question is intentionally restricted the way it is now. Modern languages (that are opinionated on readability) would likely have such a rule not just for arrays.
MisterMiyagi
CC BY-SA 4.0
2024-05-15T09:00:27.903Z
Ada *examples* probably use type aliases heavily because a) it demonstrates an extra feature (many languages don't have them at all); and b) it makes the examples (like other code) easier to read.
IMSoP
CC BY-SA 4.0
2024-05-15T09:14:52.443Z
@MisterMiyagi: for VHDL: all types, not just complex/aggregate types, period. An object declaration declares an object to be of a named (visible) 'subtype_indication', which must include a 'type_name' or a 'subtype_name' (as kaya3 suggested). But this appears *not* to be the case in (current) Ada; I haven't checked the LRM.
QF0
CC BY-SA 4.0
2024-05-15T09:16:47.157Z
@kaya3: it is true, for VHDL at least, that type annotations can only refer to types by name. However, this seems not to be true of Ada.
QF0
CC BY-SA 4.0
2024-05-15T09:30:01.310Z
@QF0 Point is, the question only asks for array types. You might want to broaden that still.
MisterMiyagi
CC BY-SA 4.0
2024-05-15T09:58:27.260Z
Incidentally, I also don't think VHDL is any more "obscure" than Ada. So I suggesting a major [edit] to the question: replace the Ada example with a VHDL one, since this is the language which actually has the restriction; talk about "type aliases" or "sub-type names" in general, not just array sub-types.
IMSoP
CC BY-SA 4.0
2024-05-15T13:28:26.117Z
I don't know if this is true of any real languages, but one reason to insist on types having names is if the language is strictly nominally typed, then two different aliases for `int[]` might not be mutually assignable. Also, if the language has no subtype relationships, then representing a type as a simple name (or a unique ID associated with that type, which can be looked up by name) makes type-checking more efficient - you don't need to walk a tree to check whether `Map<Integer, String[]>` is equal to another `Map<Integer, String[]>`.
kaya3
CC BY-SA 4.0
2024-05-15T13:39:48.873Z
@kaya3 That's a very good point. Even if different named subtypes can be considered compatible, that could be represented in the type checker as a pre-calculated set of allowed coercions, rather than ad hoc checks of compatibility between anonymous types.
IMSoP
CC BY-SA 4.0
2024-05-15T16:22:32.250Z
@kaya3 I'm pretty sure that's why Ada code generally uses type names. One of the goals in Ada's design was extreme type safety.
Barmar
CC BY-SA 4.0
2024-05-18T10:33:54.890Z
I'm confident that the actual reason is resources available at that time. IIRC, modern versions of Ada removed some of the restrictions from that time.
feldentm
# Languages in which array types must be declared before usage
Posted by: username_0 on 2024-05-14
Some languages require array types to be declared before an object of that type can be declared. In Ada, for example:
 type My_Int is range 0 .. 1000;
 type Index is range 11 .. 15;
 type My_Int_Array is array (Index) of My_Int;
 Tab : constant My_Int_Array := (2, 3, 5, 7, 11);

rather than simply
Tab : constant array(11 .. 15) of integer(0 .. 1000) := (2, 3, 5, 7, 11);

What other languages have this restriction? Is there any particularly good reason to do this, apart from just making life easier for the compiler?
**Tags:** arrays declarations
> **Comments:**
> **username_1**: Is this just about array types or any complex types in general? `constant array(11 .. 15) of integer(0 .. 1000)` is quite a mouthful, so simple readability could motivate a rule to forbid non-primitive types as part of larger expressions.  
> **username_2**: This sounds like it could be a special case of a more general rule, that type annotations can only refer to types by name. That suggests a language design reason, perhaps because allowing arbitrary syntax for types to appear in variable declarations might lead to an ambiguity if some of the same syntax might also be used for an expression.  
> **username_3**: The example given doesn't appear to be true, at least in current versions of Ada. Here's an online compiler happily accepting a fully inline array type: http://tpcg.io/_7AOR14 And I can't see any note in [this annotated reference manual](http://www.ada-auth.org/standards/22aarm/html/AA-3-6.html) that the syntax used to only allow a "subtype name" here, rather than any "subtype indication".  
> **username_0**: @IMSoP: interesting; thanks. I don't write Ada, but I do write VHDL (which was too obscure to mention). VHDL does have this restriction, but was based on Ada 83. Maybe this was added in Ada 95+, or it was considered too 'difficult' for VHDL? I have no idea. I got the example from adacore.com's section on arrays.  
> **username_0**: @MisterMiyagi: I suspect that, for VHDL (see other comment), compiler efficiency was more of a concern than readability. One of the original implementers is on record as saying that processing power in the early 80s was a factor in the language design.  
> **username_1**: @QF0 I don't want to rule out efficiency as a reason, I'm just wondering if the question is intentionally restricted the way it is now. Modern languages (that are opinionated on readability) would likely have such a rule not just for arrays.  
> **username_3**: Ada *examples* probably use type aliases heavily because a) it demonstrates an extra feature (many languages don't have them at all); and b) it makes the examples (like other code) easier to read.  
> **username_0**: @MisterMiyagi: for VHDL: all types, not just complex/aggregate types, period. An object declaration declares an object to be of a named (visible) 'subtype_indication', which must include a 'type_name' or a 'subtype_name' (as kaya3 suggested). But this appears *not* to be the case in (current) Ada; I haven't checked the LRM.  
> **username_0**: @kaya3: it is true, for VHDL at least, that type annotations can only refer to types by name. However, this seems not to be true of Ada.  
> **username_1**: @QF0 Point is, the question only asks for array types. You might want to broaden that still.  
> **username_3**: Incidentally, I also don't think VHDL is any more "obscure" than Ada. So I suggesting a major [edit] to the question: replace the Ada example with a VHDL one, since this is the language which actually has the restriction; talk about "type aliases" or "sub-type names" in general, not just array sub-types.  
> **username_2**: I don't know if this is true of any real languages, but one reason to insist on types having names is if the language is strictly nominally typed, then two different aliases for `int[]` might not be mutually assignable. Also, if the language has no subtype relationships, then representing a type as a simple name (or a unique ID associated with that type, which can be looked up by name) makes type-checking more efficient - you don't need to walk a tree to check whether `Map<Integer, String[]>` is equal to another `Map<Integer, String[]>`.  
> **username_3**: @kaya3 That's a very good point. Even if different named subtypes can be considered compatible, that could be represented in the type checker as a pre-calculated set of allowed coercions, rather than ad hoc checks of compatibility between anonymous types.  
> **username_4**: @kaya3 I'm pretty sure that's why Ada code generally uses type names. One of the goals in Ada's design was extreme type safety.  
> **username_5**: I'm confident that the actual reason is resources available at that time. IIRC, modern versions of Ada removed some of the restrictions from that time.  
---
## 0 answers
