From: deadlyhead on
Say I have a discriminated record with the following definition


-- code begins
type Discrim_Val is (Int, Str);

type Discrim_Rec (D : Discrim_Val) is record
case D is
when Int =>
Val_Int : Integer := 1_000_000;
when Str =>
Val_Str : String (1 .. 11) := "One million";
end case;
end record;
-- code ends


Is there any way to determine what the discriminant value was on a
type later on, i.e. with a dynamically allocated Discrim_Rec object
created with user input for D? A haven't found anything looking
through the RM about attributes or anything providing this
functionality.

Thanks!
-- deadlyhead
From: Jeffrey R. Carter on
On 07/19/2010 03:36 PM, deadlyhead wrote:
>
> -- code begins
> type Discrim_Val is (Int, Str);
>
> type Discrim_Rec (D : Discrim_Val) is record
> case D is
> when Int =>
> Val_Int : Integer := 1_000_000;
> when Str =>
> Val_Str : String (1 .. 11) := "One million";
> end case;
> end record;
> -- code ends
>
> Is there any way to determine what the discriminant value was on a
> type later on, i.e. with a dynamically allocated Discrim_Rec object
> created with user input for D? A haven't found anything looking
> through the RM about attributes or anything providing this
> functionality.

V : Discrim_Rec := Some_Func;

case V.D is
when Int =>
...
when Str =>
...
end case;

The discriminant is a field just like any other.

--
Jeff Carter
"How'd you like to hide the egg and gurgitate
a few saucers of mocha java?"
Never Give a Sucker an Even Break
101
From: deadlyhead on
On Jul 19, 5:26 pm, "Jeffrey R. Carter"
<spam.jrcarter....(a)spam.acm.org> wrote:
> On 07/19/2010 03:36 PM, deadlyhead wrote:
>
>
>
>
>
> > --  code begins
> > type Discrim_Val is (Int, Str);
>
> > type Discrim_Rec (D : Discrim_Val) is record
> >     case D is
> >        when Int =>
> >           Val_Int : Integer := 1_000_000;
> >        when Str =>
> >           Val_Str : String (1 .. 11) := "One million";
> >     end case;
> > end record;
> > --  code ends
>
> > Is there any way to determine what the discriminant value was on a
> > type later on, i.e. with a dynamically allocated Discrim_Rec object
> > created with user input for D?  A haven't found anything looking
> > through the RM about attributes or anything providing this
> > functionality.
>
> V : Discrim_Rec := Some_Func;
>
> case V.D is
> when Int =>
>     ...
> when Str =>
>     ...
> end case;
>
> The discriminant is a field just like any other.
>
> --
> Jeff Carter
> "How'd you like to hide the egg and gurgitate
> a few saucers of mocha java?"
> Never Give a Sucker an Even Break
> 101

Thank you! I really don't know how I missed that.
From: Maciej Sobczak on
On 20 Lip, 02:26, "Jeffrey R. Carter" <spam.jrcarter....(a)spam.acm.org>
wrote:

> The discriminant is a field just like any other.

It is read-only for the lifetime of the given object and it also
cannot be aliased.

AARM says that discriminants are components of the object, but I think
that this is a bit misleading, as their use is restricted. I think
that in some cases the compiler might completely eliminate them out of
existence as a matter of space optimization and still provide the same
syntax for reading their values.

--
Maciej Sobczak * http://www.inspirel.com

YAMI4 - Messaging Solution for Distributed Systems
http://www.inspirel.com/yami4