home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / ada / 3320 < prev    next >
Encoding:
Text File  |  1992-11-20  |  2.8 KB  |  83 lines

  1. Newsgroups: comp.lang.ada
  2. Path: sparky!uunet!noc.near.net!inmet!spock!stt
  3. From: stt@spock.camb.inmet.com (Tucker Taft)
  4. Subject: Re: null arrays
  5. Message-ID: <1992Nov20.203818.26501@inmet.camb.inmet.com>
  6. Sender: news@inmet.camb.inmet.com
  7. Nntp-Posting-Host: spock
  8. Organization: Intermetrics Inc, Cambridge MA
  9. References: <BxvtCA.LM1@dale.cts.com> <dnsurber.722098125@node_26400>
  10. Date: Fri, 20 Nov 1992 20:38:18 GMT
  11. Lines: 70
  12.  
  13. In article <dnsurber.722098125@node_26400> 
  14.   dnsurber@lescsse.jsc.nasa.gov (Douglas N. Surber) writes:
  15.  
  16. >In <BxvtCA.LM1@dale.cts.com> jhb@dale.cts.com (John Bollenbacher) writes:
  17. >
  18. >>procedure TEST is
  19. >
  20. >>  subtype T is NATURAL range 0 .. 10;
  21. >
  22. >>  type ARR is array (T range <>) of BOOLEAN;
  23. >>  
  24. >>  type A(N : T := 0) is record 
  25. >>    DATA : ARR(1..N);
  26. >>  end record;  
  27. >>  I : INTEGER;
  28. >>  O : A := (3, (TRUE, FALSE, TRUE));
  29. >>  
  30. >>  N1 : constant ARR := O.DATA(1..0) & O.DATA(1..0);
  31. >>  N2 : constant ARR := O.DATA(1..0) & O.DATA(3..2);
  32. >
  33. >N2'first = 3 (LRM 4.5.3 4)
  34. >
  35. >>begin
  36. >>  I := N1'LENGTH; -- I = 0 
  37. >>  I := N2'LENGTH; -- I = 0
  38. >>  O := (0, N1);   -- does not raise constraint
  39. >>  O := (0, N2);   -- raises constraint
  40. >O.Data'first /= N2'first thus raises constraint error (LRM 4.3.1 3, 3.3 4)
  41. >
  42. >>end TEST; 
  43. >
  44. >The trick here is that the constraint error is raised in forming the
  45. >aggregate, not in the assignment.  LRM 4.3.1 3 says "A check is made that
  46. >the value of each subcomponent of the aggregate _belongs_ to the subtype
  47. >of this component."  The magic word is "belongs".  LRM 3.3 4 says "a value
  48. >is said to belong to a subtype of a given type if it belongs to the type
  49. >and satisfies the constraint." The bounds of N2, i.e. 3 and 2, do not
  50. >satisfy the constraint of the aggregate, i.e. in the range 1 .. 0.  If this
  51. >were an array assignment rather than an aggregate, it would work.
  52.  
  53. This problem is alleviated in Ada 9X by providing "sliding" (implicit
  54. array subtype conversion) in essentially all contexts where
  55. it might be useful.  In particular, in Ada 9X, the bounds of
  56. N2 (i.e. 3..2) would "slide" to match the bounds of the DATA
  57. component (i.e. 1..0) as part of constructing the aggregate.
  58.  
  59. Until you convince your implementor to provide a "-9X" mode
  60. in their compiler ;-), you might want to define a function that "slides"
  61. its parameter and then returns it.  For example, here is a function
  62. that slides its parameter to have a low bound of 1 (if needed, you
  63. could define one that took the low bound desired as an additional
  64. parameter):
  65.  
  66.    function Slide(A : ARR) return ARR is
  67.        subtype S is ARR(1..A'Length);
  68.    begin
  69.        return S(A);  -- Explicit array subtype conversion
  70.    end Slide;
  71.  
  72. A "pragma INLINE" might be appropriate here.
  73.  
  74. >Douglas Surber
  75. >Lockheed
  76. >Houston, TX
  77.  
  78. S. Tucker Taft    stt@inmet.com
  79. Ada 9X Mapping/Revision Team
  80. Intermetrics, Inc.
  81. Cambridge, MA  02138
  82.