comp.lang.ada
 help / color / mirror / Atom feed
From: "G.B." <bauhaus@notmyhomepage.invalid>
Subject: Re: Assignment access type with discriminants
Date: Wed, 22 Mar 2023 15:10:44 +0100	[thread overview]
Message-ID: <tvf294$ldho$1@dont-email.me> (raw)
In-Reply-To: <tveh6u$ihpu$1@dont-email.me>

On 22.03.23 10:19, Dmitry A. Kazakov wrote:
> I stumbled on a curious fact.
> 
> The value of an object with a discriminant can be changed to a value with a different discriminant if the type's discriminants are defaulted.
> 
> Right?
> 
> Wrong! Not through an access type!
> 
> procedure Test is
>     type F is (F1, F2, F3);
>     type Foo (K : F := F1) is record
>        case K is
>           when F1 =>
>              X1 : Integer;
>           when F2 =>
>              X2 : Float;
>           when F3 =>
>              X3 : String (1..2);
>        end case;
>     end record;
>     type Foo_Ptr is access all Foo;
>     X : aliased Foo;
>     P : Foo_Ptr := X'Access;
> begin
>     X := (F2, 1.0);   -- OK
>     P.all := (F1, 3); -- Error!
> end Test;

Some experiments point at the general access type.

    type Foo_Ptr is access Foo; -- sans `all`
    X : Foo;
    P : Foo_Ptr := new Foo;
    type Foo1 is new Foo_Ptr (K => F1);
begin
    X := (F2, 1.0);   -- OK
    P.all := (F1, 3); -- _no_ Error!
    Foo1 (P).all := (F1, 3);
end Test;

(Doesn't rejection for general access types seem reasonable
if assignment would otherwise require adjusting the storage
layout of a variable, including all access paths to components?
Just guessing.)


  parent reply	other threads:[~2023-03-22 14:10 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-22  9:19 Assignment access type with discriminants Dmitry A. Kazakov
2023-03-22  9:31 ` Björn Lundin
2023-03-22 14:10 ` G.B. [this message]
2023-03-23 11:51   ` Dmitry A. Kazakov
2023-03-23 16:53     ` AdaMagica
2023-03-23 18:09       ` Niklas Holsti
2023-03-23 17:04 ` J-P. Rosen
2023-03-23 18:55   ` Niklas Holsti
2023-03-23 19:53     ` Dmitry A. Kazakov
2023-03-24  9:41     ` J-P. Rosen
2023-03-25  8:51       ` Randy Brukardt
replies disabled

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