TEAM-ADA Archives

Team Ada: Ada Programming Language Advocacy

TEAM-ADA@LISTSERV.ACM.ORG

Options: Use Forum View

Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Jeffrey Carter <[log in to unmask]>
Reply To:
Jeffrey Carter <[log in to unmask]>
Date:
Fri, 16 Feb 2001 18:57:45 -0600
Content-Type:
text/plain
Parts/Attachments:
text/plain (70 lines)
"Deller, Steve" wrote:
>
> It might be reasonable to consider adding:
>
>   generic
>      type Source(<>) is limited private;
>      type Target(<>) is limited private;
>   function Checked_Conversion( S : Source ) return Target ;
>   pragma Convention(Intrinsic, Checked_Conversion);
>   pragma Pure(Checked_Conversion);
>
> to the language.  The function would return "Constraint_Error" if any scalar
> component of the return value would return a false 'Valid.
>
> I had thought one could write such a function, but it is virtually
> impossible to write an Ada program to "walk" an arbitrary type to find all
> components, and even if possible, the limited private target makes it
> impossible to create a temporary object that could be used with 'Valid.

You can create something like

package Checked_Conversions is
   pragma Pure;

   generic -- Checked_Discrete_Conversion
     type Source (<>) is limited private;
     type Target is (<>);
   function Checked_Discrete_Conversion (S : Source) return Target;

   generic -- Checked_Float_Conversion
      type Source (<>) is limited private;
      type Target is digits <>;
   function Checked_Float_Conversion (S : Source) return Target;

   generic -- Checked_Fixed_Conversion
      type Source (<>) is limited private;
      type Target is delta <>;
   function Checked_Fixed_Conversion (S : Source) return Target;

   generic -- Checked_Decimal_Conversion
      type Source (<>) is limited private;
      type Target is delta <> digits <>;
   function Checked_Decimal_Conversion (S : Source) return Target;
end Checked_Conversions;

to handle all possible scalar types. You could ease the conversion of
composite types by creating something like

generic -- Checked_Composite_Conversion
   type Source (<>) is limited private;
   type Target (<>) is limited private;

   with procedure Check_Subcomponents (Value : in Target);
   -- Applies 'Valid to every (sub)component of Value
   -- Raises Constraint_Error if any are False
   -- Has no effect otherwise
function Checked_Composite_Conversion (S : Source) return Target;

You can declare an object of type Target, assuming an instantiation of
Ada.Unchecked_Conversion for Source and Target:

   T : Target renames To_Target (S);

This could then be passed to Check_Subcomponents and returned.

--
Jeff Carter
"Hello! Smelly English K...niggets."
Monty Python & the Holy Grail

ATOM RSS1 RSS2