comp.lang.ada
 help / color / mirror / Atom feed
From: "Jeffrey R. Carter" <spam@spam.com>
Subject: Re: Discriminated types with default discriminants
Date: Thu, 03 Nov 2005 18:51:32 GMT
Date: 2005-11-03T18:51:32+00:00	[thread overview]
Message-ID: <U_saf.5294$Rl1.469@newsread1.news.pas.earthlink.net> (raw)
In-Reply-To: <dkdbjk$cd8$1@sunnews.cern.ch>

Maciej Sobczak wrote:

>    type Discriminated(Size : Integer := 10) is
>       record
>          Value : String (1..Size);
>       end record;

Aside : begin

When I see something like this, I always wonder: What does a negative Size mean?

To my mind, a negative Size is meaningless, and should not be allowed:

Size : Natural

end Aside;

There are 3 cases with discriminants:

1. No default (object must be declared with explicit value)
2. Default, and object declared with explicit value
3. Default, and object declared without explicit value

The 1st 2 cases are equivalent. The object must always have the same value for 
the discriminant. It can never change. The object is constrained.

The last case means the discriminant can change. The object is unconstrained.

Given

subtype V_Index is Natural range 0 .. 255;

type V_String (Length : V_Index := 0) is record
    Data : String (1 .. Length) := String'(1 .. Length => 'J');
end record;

V : V_String;

one can later change the Length of V:

V := V_String'(Length => 23, Data => String'(1 .. 23 => 'C') );

There are 2 ways the compiler may implement this. Many compilers, including 
GNAT, allocate enough storage for the largest value, and then use parts of this 
storage depending on the current value of the discriminant. If the largest value 
is too big, such a declaration results in Storage_Error being raised.

A few compilers will add a level of indirection, and store the variable-sized 
stuff elsewhere, reallocating storage when the amount available is not big 
enough. I think RR Software's Janus Ada compiler does this. With this scheme, 
you only get Storage_Error if you actually try to store a value that is too big.

As with most implementation decisions, it's a trade off among complexity, space, 
and time.

-- 
Jeff Carter
"Now go away or I shall taunt you a second time."
Monty Python & the Holy Grail
07



  parent reply	other threads:[~2005-11-03 18:51 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-03 15:50 Discriminated types with default discriminants Maciej Sobczak
2005-11-03 16:06 ` Martin Krischik
2005-11-03 23:10   ` Robert A Duff
2005-11-04  5:01     ` Jeffrey R. Carter
2005-11-03 16:21 ` Ed Falis
2005-11-03 17:28 ` Dmitry A. Kazakov
2005-11-03 18:51 ` Jeffrey R. Carter [this message]
2005-11-03 23:08   ` Robert A Duff
2005-11-04  0:08     ` Adam Beneschan
2005-11-29  2:49       ` Randy Brukardt
2005-12-02 18:22         ` Robert A Duff
2005-11-04  9:18   ` Maciej Sobczak
2005-11-04  3:27 ` Brian May
2005-11-04 13:09   ` Stephen Leake
2005-11-04 17:58   ` Martin Krischik
replies disabled

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