comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: Variant record assignment fails discriminant check
Date: Wed, 2 Sep 2009 08:33:07 -0700 (PDT)
Date: 2009-09-02T08:33:07-07:00	[thread overview]
Message-ID: <f211c2bb-1748-4d3c-ac00-8b4619f16ce5@m7g2000prd.googlegroups.com> (raw)
In-Reply-To: 2e307dac-ae36-46a3-813f-09271215cca3@o9g2000yqj.googlegroups.com

On Sep 2, 8:09 am, vldmr <vldm...@gmail.com> wrote:
> On Sep 2, 9:52 am, Adam Beneschan <a...@irvine.com> wrote:
>
> > On Sep 2, 7:25 am, vldmr <vldm...@gmail.com> wrote:
>
> > Only if the actual parameter is mutable when you call the procedure.
> ...
>
> > OK, so I'll bet your next question is: So how do I declare an object
> > to be unconstrained (mutable), but give its discriminant an initial
> > value other than the default (X520CommonName_teletexString)?
>
> >    WXY : t_X520CommonName := (choice_Id =>
> > X520CommonName_printableString,
> >                               others => <>);
>
> >                                           -- Adam
>
> Oh, thank, make sense now. At least I see where my problem is. In my
> case WXY is actually declared as access to record, and is initialized
> using Unchecked_Conversion from external memory address, pointing to
> zero filled memory.
>
> Any advice how to declare an access to variant record to be mutable?

I don't think the language will let you do this, directly.  If you
declare a type "type acc_t_X520CommonName is access all
t_X520CommonName", and an object Acc of this access type, then Acc.all
is assumed to be constrained, and if you pass Acc.all to your
procedure, the procedure can't change it.  (The original motivation
was, I think, that you can declare a constrained subtype of the access
type; if you have an access object of that constrained subtype and
make it point to an object with the correct discriminant, it would be
bad if the language let you change the discriminant via a different,
unconstrained, access object.  There have been some language changes
regarding discriminants on access types, and I'm not familiar with all
of them, so maybe you can do things you previously couldn't.  I'm not
sure.)

However, you can get around this by embedding the variant record
inside another record:

   type Rec is record
      Name : t_X520CommonName;
   end record;
   type Acc_Rec is access all Rec;

Now, if X has type Acc_Rec, you can pass X.Name to your procedure, and
it will be unconstrained (and therefore mutable).

This is a workaround that I've used extensively in my own code, by the
way.

                                      -- Adam




  reply	other threads:[~2009-09-02 15:33 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-02 14:25 Variant record assignment fails discriminant check vldmr
2009-09-02 14:52 ` Adam Beneschan
2009-09-02 15:09   ` vldmr
2009-09-02 15:33     ` Adam Beneschan [this message]
2009-09-02 16:09       ` vldmr
2014-05-12  0:31 ` maniyazhagan
2014-05-12  0:32 ` maniyazhagan
replies disabled

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