From: collard@software.org (David Collard)
Subject: Re: implicit array type conversion
Date: 26 Jul 90 20:36:49 GMT [thread overview]
Message-ID: <1469@software.software.org> (raw)
In-Reply-To: 1990Jul26.132742.3828@planck.uucp
In article <1990Jul26.132742.3828@planck.uucp> acsu.buffalo.edu!planck!hercules!westley () writes:
> Why is it that implicit array type conversion does not apply to the
> following situation? I can't find anything in the RM that disallows
> this situation for implicit conversion.
>
> procedure Strings is
>
> type STRING (Len : NATURAL) is
> record
> S : Standard.STRING(1..Len);
> end record;
>
> A : Standard.STRING(7..7);
> B : STRING(1);
^^^^^^^^^^^^^^ At this point B is constrained to Len = 1.
Which is why a constraint error is raised later.
>
> begin -- Strings
>
> A := "X";
> B := (A'length, A);
>
> end Strings;
>
An aggregate assignment which overrides the discriminant of a record is
only allowed if the discriminant has a default and the object is not
constrained when it is declared, i.e
type STRING(Len : Natural := 0) is
record
S : Standard.STRING(1..Len);
end record;
B : String;
If you change your declaration to this, then the assignment will not
raise a constraint error.
HOWEVER it still will not work because when the discriminant does have
a default and an object is declared using the default , then the compiler
will try to allocate enough space for the maximum value for the discriminant
(probably over 2 billion if Natural is 32 bits) which is
1) Too much memory
2) Is beyond the maximum length for a string on some machines.
What will work is:
type String_Index is range 0..100;
type String(Len : String_Index := 0) is
record
S : Standard.String(1..Natural(Len));
end record;
B : String;
But note that if the range of String_Index is large, you may be using
a lot more memory than is apparent.
The discriminated record is not intended to support dynamic allocation
of different sized objects. If you really want dynamic allocation then
use an access type.
Good Luck
--
-----------------------------------------------------------------------
D. Thor Collard Internet: collard@software.org
Software Productivity Consortium UUNET: ...!uunet!software!collard
2214 Rock Hill Rd, Herndon VA 22070
next prev parent reply other threads:[~1990-07-26 20:36 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
1990-07-26 13:27 implicit array type conversion
1990-07-26 20:36 ` David Collard [this message]
1990-07-27 17:13 ` Michael Feldman
-- strict thread matches above, loose matches on Subject: below --
1990-07-26 20:56 "Norman H. Cohen"
[not found] <132742@<1990Jul26>
1990-07-27 16:01 ` stt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox