3
$\begingroup$

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?

$\endgroup$
15
  • 1
    $\begingroup$ 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. $\endgroup$ Commented May 14, 2024 at 10:59
  • 4
    $\begingroup$ 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: tpcg.io/_7AOR14 And I can't see any note in this annotated reference manual that the syntax used to only allow a "subtype name" here, rather than any "subtype indication". $\endgroup$ Commented May 14, 2024 at 18:55
  • 1
    $\begingroup$ @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. $\endgroup$ Commented May 15, 2024 at 8:47
  • 1
    $\begingroup$ @QF0 Point is, the question only asks for array types. You might want to broaden that still. $\endgroup$ Commented May 15, 2024 at 9:30
  • 3
    $\begingroup$ 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. $\endgroup$ Commented May 15, 2024 at 9:58

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.