comp.lang.ada
 help / color / mirror / Atom feed
* defaults on discriminants
@ 1994-11-15 22:54 Gene McCulley
  1994-11-20 17:14 ` Robert Dewar
  0 siblings, 1 reply; 2+ messages in thread
From: Gene McCulley @ 1994-11-15 22:54 UTC (permalink / raw)


The following compiles fine under LegacyAda 1.4.0 and GNAT but does not under
Rational Apex 1.4.1 on the RS/6000:

procedure Dummytest is

    type Integer_Array is array (Natural range <>) of Integer;

    type Foo_Record_Type (Count1 : Natural := 1) is
        record
            Things : Integer_Array (1 .. Count1);
    end record;

    My_Foo : Foo_Record_Type (5);
begin
    My_Foo.Things (1) := 5;
end Dummytest;

!!! [MID] Program will raise CONSTRAINT_ERROR (Range_Error) at "type
FOO_RECORD_TYPE (COUNT1 : NATURAL := 1) is " (DUMMYTEST'BODY.2D)

The Apex generated executable says:
Exception CONSTRAINT_ERROR (Range_Error) raised
and then shows the type definition for Foo_Record_Type in the debugger.

Apex compiles this fine if I give no default for the discriminant.
Shouldn't I be able to give it a default?



^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: defaults on discriminants
  1994-11-15 22:54 defaults on discriminants Gene McCulley
@ 1994-11-20 17:14 ` Robert Dewar
  0 siblings, 0 replies; 2+ messages in thread
From: Robert Dewar @ 1994-11-20 17:14 UTC (permalink / raw)


This is such a common question that it should be in the Ada FAQ

When you give a default discriminant, then one method (I actually think it
is the preferred method) of implementation is to allocate the maximum
possible length. Since your discriminant is of type Natural, this clearly
won't work!

GNAT may compile it, but it won't run it, and indeed I consider it a GNAT
bug (on the todo list) that no warning is issued at compile time for this
misuse.

Some compilers, notably Alsys and RR, have at least partially "solved"
this problem by introducing hidden pointers, but this to me is an undesirable
implementation choice.

First, it means there is hidden heap activity, which seems undesirable. In
a language where pointers are explicit, it is generally a good idea if
allocation is also explicit, and certainly for real-time work, hidden anything
is worrisome.

Second, it is not easy to do uniformly. Alsys ends up introducing arbitrary
restrictions on the composition of such types (try making an array of them),
and RR introduces non-contiguous representations, which are legal but
troublesome.

To "solve" the problem yourself, just declare a reasonable maximum length,
and use a subtype representing this length as the subtype of the
discriminant.




^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1994-11-20 17:14 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1994-11-15 22:54 defaults on discriminants Gene McCulley
1994-11-20 17:14 ` Robert Dewar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox