comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@nospam.please>
Subject: Re: Translating an embedded C algorithm
Date: Thu, 18 Jan 2007 23:25:58 +0200
Date: 2007-01-18T23:25:58+02:00	[thread overview]
Message-ID: <45afe4a6$0$22512$39db0f71@news.song.fi> (raw)
In-Reply-To: <wccy7o05l73.fsf@shell01.TheWorld.com>

Robert A Duff wrote:
> ...
> 
> Also, I think it would be overkill to put both ways of dealing with
> invalid results in an example in a book.  I'd suggest just using the
> exception method, and ignore the rather complex idea of variant
> records.

You may be right that having both methods is too much for this example, 
but I see an important difference in capability and applications between 
variant records and exceptions:

- Exceptions are dynamic and evanescent; they are useful for
   forcing the program to take immediate note of an unexpected
   situation that makes it impossible or incorrect to continue
   with business as usual. In this example, the exception says
   "your present attempt to convert this ADC count into a
   temperature is impossible and I will not let you continue
   as if it had succeeded".

- Variant records can be static and persistent; they are useful
   for recording the fact that some data are available, others
   not. In this example, an ADC.Thermometer.Reading_T could
   be stored in some status record, or telemetry packet, and
   could be consulted later, by other parts of the program, to
   get the temperature or notice the out-of-range condition.
   A value with In_Range = False says "no current temperature
   is available because the last attempt to read the temperature
   gave an out-of-range result".

Thus I think that both methods have their uses, often in the same 
program but at different points.

Of course one could store a non-variant record with a Boolean in-range 
flag and a temperature component, but this would not prevent some part 
of the program from accessing the temperature component even if its 
value is wrong because of an out-of-range condition. As you know (but 
the OP perhaps does not) the variant record form guards against such 
errors by raising Constraint_Error if the program tries to access a 
component that is not present in the current variant.

The variant record approach could also be extended a bit to bring 
enumerated types into the example. Instead of the current Boolean 
discriminant (which is a bit ugly in the "case" constructs) we could 
define, in ADC.Thermometer:

    type Status_T is (Out_Of_Range, In_Range, Under_Min, Over_Max);
    --
    -- The general status of an ADC thermometer reading.
    --
    -- Out_Of_Range
    --    The ADC count was not within the expected range,
    --    and saturation was not requested, so no temperature
    --    is available.
    -- In_Range
    --    The ADC count was within the expected range so the
    --    temperature is available.
    -- Under_Min
    --    The ADC count was below the minimum expected value.
    --    The temperature was saturated at its minimum value.
    -- Over_Max
    --    The ADC count was above the maximum expected value.
    --    The temperature was saturated at its maximum value.

    type Reading_T (Status : Status_T := Out_Of_Range) is record
       case Status is
       when Out_Of_Range =>
          null;
       when In_Range | Under_Min | Over_Max =>
          Temperature : Temperatures.Celsius_T;
       end case;
    end record;

(I expect someone will object that one should use the discriminant only 
to discriminate the variant forms (temperature available or not) and not 
to differentiate other status (temperature saturated or not). That is a 
valid philosophy, and I certainly consider that alternative when I 
design a variant record type, but I defend my right to use the above 
approach, too, when it suits me. :-)

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



  parent reply	other threads:[~2007-01-18 21:25 UTC|newest]

Thread overview: 102+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-01-15 14:36 Translating an embedded C algorithm Talulah
2007-01-16  0:06 ` Jeffrey Carter
2007-01-16  1:10   ` Marc A. Criley
2007-01-16  2:21   ` Cesar Rabak
2007-01-16 10:40     ` Markus E Leypold
2007-01-16 14:48       ` Larry Kilgallen
2007-01-16 17:32     ` Jeffrey Carter
2007-01-16 18:04       ` Cesar Rabak
2007-01-17  0:09         ` Jeffrey Carter
2007-01-17  1:07           ` Cesar Rabak
2007-01-16  4:37   ` Alexander E. Kopilovich
2007-01-16 13:37     ` Cesar Rabak
2007-01-16 23:47     ` Simon Wright
2007-01-17  1:02       ` Cesar Rabak
2007-01-16  3:50 ` Vo, Anh (US SSA)
2007-01-16 12:15   ` Niklas Holsti
2007-01-16 23:50     ` Simon Wright
2007-01-18 16:17     ` Niklas Holsti
2007-01-18 16:41       ` Ludovic Brenta
2007-01-18 20:02         ` Niklas Holsti
2007-01-18 22:25         ` Cesar Rabak
2007-01-19  8:32           ` Niklas Holsti
2007-01-19 19:15             ` Cesar Rabak
2007-01-19 20:49             ` Simon Wright
2007-01-18 16:55       ` Robert A Duff
2007-01-18 18:54         ` Jeffrey Carter
2007-01-19  0:45           ` Robert A Duff
2007-01-18 21:25         ` Niklas Holsti [this message]
2007-01-19  0:50           ` Robert A Duff
2007-01-19  4:43           ` Jeffrey Carter
2007-01-18 18:43       ` Jeffrey Carter
2007-01-18 20:19         ` Niklas Holsti
2007-01-18 20:30       ` Niklas Holsti
2007-01-18 23:34       ` Cesar Rabak
2007-01-19  8:57         ` Niklas Holsti
2007-01-19  2:11       ` Steve Whalen
2007-01-19 10:27         ` Niklas Holsti
2007-01-16 13:32   ` Cesar Rabak
2007-01-16 14:47   ` Gautier
2007-01-16 15:15     ` Cesar Rabak
2007-01-16 15:16     ` Jean-Pierre Rosen
2007-01-16 16:12       ` Ludovic Brenta
2007-01-16 17:10         ` Georg Bauhaus
2007-01-16 22:32           ` Ludovic Brenta
2007-01-17 20:22             ` Georg Bauhaus
2007-01-18  9:23               ` Ludovic Brenta
2007-01-16 17:12         ` Cesar Rabak
2007-01-16 17:20           ` Frank J. Lhota
2007-01-16 18:09             ` Cesar Rabak
2007-01-16 17:36           ` Dmitry A. Kazakov
2007-01-16 18:08             ` Cesar Rabak
2007-01-16 18:48               ` Dmitry A. Kazakov
2007-01-16 20:03                 ` Cesar Rabak
2007-01-18 19:33                   ` Björn Persson
2007-01-18 22:32                     ` Cesar Rabak
2007-01-19 20:26                       ` Björn Persson
2007-01-19 23:25                         ` Cesar Rabak
2007-01-19  7:15                     ` Maciej Sobczak
2007-01-19 20:27                       ` Björn Persson
2007-01-19 20:34                         ` Robert A Duff
2007-01-17 13:48           ` Maciej Sobczak
2007-01-17 23:32             ` Translating an embedded C algorithm -- OT Cesar Rabak
2007-01-18  8:56               ` Talulah
2007-01-18 22:05                 ` Cesar Rabak
2007-01-18  9:03               ` Maciej Sobczak
2007-01-18 10:22                 ` Alex R. Mosteo
2007-01-18 18:34                 ` Jeffrey Carter
2007-01-18 22:26                   ` Cesar Rabak
2007-01-19  4:45                     ` Jeffrey Carter
2007-01-18 22:18                 ` Cesar Rabak
2007-01-19 20:53                   ` Simon Wright
2007-01-16 15:55   ` Translating an embedded C algorithm Cesar Rabak
2007-01-17  3:00     ` Vo, Anh (US SSA)
2007-01-17 10:48       ` Cesar Rabak
2007-01-17 11:44       ` Niklas Holsti
2007-01-17 13:31         ` Talulah
2007-01-17 19:20           ` Jeffrey Carter
2007-01-18 14:19             ` Talulah
2007-01-18 15:28               ` Jean-Pierre Rosen
2007-01-18 23:27                 ` Cesar Rabak
2007-01-18 18:51               ` Jeffrey Carter
2007-01-18 22:30                 ` Cesar Rabak
2007-01-19  4:48                   ` Jeffrey Carter
2007-01-19 19:13                     ` Cesar Rabak
2007-01-20 20:56                       ` Jeffrey Carter
2007-01-19  2:21                 ` Alexander E. Kopilovich
2007-01-19  3:25                   ` Larry Kilgallen
2007-01-20  0:46                     ` Alexander E. Kopilovich
2007-01-20 13:03                       ` Larry Kilgallen
2007-01-20 16:54                         ` Alexander E. Kopilovich
2007-01-20 23:53                           ` Larry Kilgallen
2007-01-20 21:02                         ` Jeffrey Carter
2007-01-25 21:59                     ` Markus E Leypold
2007-01-26  4:06                       ` Larry Kilgallen
2007-01-26 11:26                         ` Markus E Leypold
2007-01-26 12:25                           ` Cesar Rabak
2007-01-19  4:52                   ` Jeffrey Carter
2007-01-19 10:13                   ` Warner BRUNS
2007-01-19 14:54                   ` Robert A Duff
2007-01-19  4:08 ` Steve
2007-01-19 20:41   ` Simon Wright
  -- strict thread matches above, loose matches on Subject: below --
2007-01-17  7:07 AW: " Grein, Christoph (Fa. ESG)
2007-01-17 10:26 ` Ludovic Brenta
2007-01-17 16:44   ` Markus E Leypold
2007-01-18  8:49     ` Ludovic Brenta
2007-01-19  9:33       ` Stephen Leake
2007-01-19 19:23         ` Cesar Rabak
2007-01-19 20:27           ` Robert A Duff
2007-01-20  9:54             ` Dmitry A. Kazakov
replies disabled

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