comp.lang.ada
 help / color / mirror / Atom feed
* Choice of variant record discriminant (design question)
@ 2005-10-26 12:21 Jacob Sparre Andersen
  2005-10-26 13:14 ` christoph.grein
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jacob Sparre Andersen @ 2005-10-26 12:21 UTC (permalink / raw)


I have declared a record type like this:

   type Preprocessed_Parameters (Measurement_Count : Natural := 0) is
      record
         case Measurement_Count is
            when 0 =>
               null;
            when others =>
               Mean_Value      : Scalar;
               Scaled_Variance : Scalar;
         end case;
      end record;

I would like to return an object of that type from a function using
this piece of code:

   if Count = 0 then
      return (Measurement_Count => 0);
   else
      [...]
      return (Measurement_Count => Count,
              Mean_Value        => Mean,
              Scaled_Variance   => Sum);
   end if;

I can't do this, since the discriminant has to be static.  The obvious
solution to the problem is to change the record declaration into:

   type Preprocessed_Parameters (Empty : Boolean := True) is
      record
         case Empty is
            when True =>
               null;
            when False =>
               Measurement_Count : Positive;
               Mean_Value        : Scalar;
               Scaled_Variance   : Scalar;
         end case;
      end record;

but I have a feeling there ought to be a more elegant solution.  Does
somebody have a suggestion or two?

Jacob
-- 
ACRONYM: A Contrived Reduction Of Nomenclature Yielding Mnemonics



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Choice of variant record discriminant (design question)
  2005-10-26 12:21 Choice of variant record discriminant (design question) Jacob Sparre Andersen
@ 2005-10-26 13:14 ` christoph.grein
  2005-10-26 13:26   ` Jacob Sparre Andersen
  2005-10-26 17:52 ` Jeffrey R. Carter
  2005-10-26 19:52 ` Randy Brukardt
  2 siblings, 1 reply; 6+ messages in thread
From: christoph.grein @ 2005-10-26 13:14 UTC (permalink / raw)


Jacob Sparre Andersen wrote:
> I have declared a record type like this:
>
>    type Preprocessed_Parameters (Measurement_Count : Natural := 0) is
>       record
>          case Measurement_Count is
>             when 0 =>
>                null;
>             when others =>
>                Mean_Value      : Scalar;
>                Scaled_Variance : Scalar;
>          end case;
>       end record;
>
> I would like to return an object of that type from a function using
> this piece of code:
>
declare
  Parm: Preprocessed_Parameters (Measurement_Count => Count);
begin
>    if Count = 0 then
       null;
>    else
>       [...]
       Parm.Mean_Value := Mean;
       Parm.Scaled_Variance := Sum;
       return Parm;
>    end if;
> 
> I can't do this, since the discriminant has to be static.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Choice of variant record discriminant (design question)
  2005-10-26 13:14 ` christoph.grein
@ 2005-10-26 13:26   ` Jacob Sparre Andersen
  0 siblings, 0 replies; 6+ messages in thread
From: Jacob Sparre Andersen @ 2005-10-26 13:26 UTC (permalink / raw)


christoph.grein@eurocopter.com writes:

> declare
>   Parm: Preprocessed_Parameters (Measurement_Count => Count);
> begin
>>    if Count = 0 then
>        null;
>>    else
>>       [...]
>        Parm.Mean_Value := Mean;
>        Parm.Scaled_Variance := Sum;
>        return Parm;
>>    end if;

Thanks!  (I wonder why I didn't remember that)

Jacob
-- 
"The three principal virtues of a programmer are Laziness,
 Impatience, and Hubris"                       -- Larry Wall



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Choice of variant record discriminant (design question)
  2005-10-26 12:21 Choice of variant record discriminant (design question) Jacob Sparre Andersen
  2005-10-26 13:14 ` christoph.grein
@ 2005-10-26 17:52 ` Jeffrey R. Carter
  2005-10-26 19:52 ` Randy Brukardt
  2 siblings, 0 replies; 6+ messages in thread
From: Jeffrey R. Carter @ 2005-10-26 17:52 UTC (permalink / raw)


Jacob Sparre Andersen wrote:

> I have declared a record type like this:
> 
>    type Preprocessed_Parameters (Measurement_Count : Natural := 0) is
>       record
>          case Measurement_Count is
>             when 0 =>
>                null;
>             when others =>
>                Mean_Value      : Scalar;
>                Scaled_Variance : Scalar;
>          end case;
>       end record;

Your record has 2 variants, so your discriminant should have 2 values.

> I can't do this, since the discriminant has to be static.  The obvious
> solution to the problem is to change the record declaration into:
> 
>    type Preprocessed_Parameters (Empty : Boolean := True) is
>       record
>          case Empty is
>             when True =>
>                null;
>             when False =>
>                Measurement_Count : Positive;
>                Mean_Value        : Scalar;
>                Scaled_Variance   : Scalar;
>          end case;
>       end record;
> 
> but I have a feeling there ought to be a more elegant solution.  Does
> somebody have a suggestion or two?

Now you have a discriminant with 2 values controlling a record with 2 variants. 
I find that more elegant than your 1st version.

-- 
Jeff Carter
"We burst our pimples at you."
Monty Python & the Holy Grail
16



^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Choice of variant record discriminant (design question)
  2005-10-26 12:21 Choice of variant record discriminant (design question) Jacob Sparre Andersen
  2005-10-26 13:14 ` christoph.grein
  2005-10-26 17:52 ` Jeffrey R. Carter
@ 2005-10-26 19:52 ` Randy Brukardt
  2005-10-31 15:53   ` Jacob Sparre Andersen
  2 siblings, 1 reply; 6+ messages in thread
From: Randy Brukardt @ 2005-10-26 19:52 UTC (permalink / raw)


"Jacob Sparre Andersen" <sparre@nbi.dk> wrote in message
news:m2acgwmpsu.fsf@hugin.crs4.it...
...
> but I have a feeling there ought to be a more elegant solution.  Does
> somebody have a suggestion or two?

I have to wonder if the variant is worth it. There is not going to be any
space savings, and in typical use, there isn't going to be much increase in
safety, either. So I'd probably give up on the variant in this case, because
I'd rather keep the aggregates (they are big help when a component needs to
be added). But only you can weight that trade-off, because the relative
value of the two options is something that depends on the components and the
intended uses. (If the "empty" variant wasn't going to be empty in future
version, for instance, the variant would make more sense.)

                        Randy.







^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Choice of variant record discriminant (design question)
  2005-10-26 19:52 ` Randy Brukardt
@ 2005-10-31 15:53   ` Jacob Sparre Andersen
  0 siblings, 0 replies; 6+ messages in thread
From: Jacob Sparre Andersen @ 2005-10-31 15:53 UTC (permalink / raw)


Randy Brukardt wrote:

> I have to wonder if the variant is worth it. There is not going to
> be any space savings,

Right.  But I hardly ever use variant records to save space.

> and in typical use, there isn't going to be much increase in safety,
> either.

Not that much, no.  But I prefer to let the code match the logic of
the problem.

> So I'd probably give up on the variant in this case, because I'd
> rather keep the aggregates (they are big help when a component needs
> to be added). But only you can weight that trade-off, because the
> relative value of the two options is something that depends on the
> components and the intended uses. (If the "empty" variant wasn't
> going to be empty in future version, for instance, the variant would
> make more sense.)

In this case, the null variant is most unlikely to end up containing
data later, so I can not use the argument that I may save space later
using this design.

If I didn't use that Ada allows me to encode the logic of my problem
in the source, couldn't I just as well use C or Fortran?

Thanks for the advice,

Jacob
-- 
"Only Hogwarts students really need spellcheckers"
                                -- An anonymous RISKS reader



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2005-10-31 15:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-10-26 12:21 Choice of variant record discriminant (design question) Jacob Sparre Andersen
2005-10-26 13:14 ` christoph.grein
2005-10-26 13:26   ` Jacob Sparre Andersen
2005-10-26 17:52 ` Jeffrey R. Carter
2005-10-26 19:52 ` Randy Brukardt
2005-10-31 15:53   ` Jacob Sparre Andersen

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