comp.lang.ada
 help / color / mirror / Atom feed
From: Shark8 <OneWingedShark@gmail.com>
Subject: Re: Effective use of derived types
Date: Fri, 25 Jul 2014 21:34:40 -0600
Date: 2014-07-25T21:34:40-06:00	[thread overview]
Message-ID: <klFAv.32490$nx3.20166@fx29.iad> (raw)
In-Reply-To: <lqu19o$k7a$1@speranza.aioe.org>

On 25-Jul-14 10:40, Victor Porton wrote:
> I write an Ada library (in fact thick bindings to a C library), which (among
> other) deals with some strings, including URIs.
>
> Should I introduce new type for URI strings?
>
> type URI_String is new String;
>
> Or it is an overkill?


It depends on how you are going to use it.
One thing to keep in mind us that you cannot overload on subtypes, but 
can on new [derived] types.

Example:

-- The Compiler cannot distinguish which function should be called.
Function "+"(Left, Right : Natural) return Natural;
Function "+"(Left, Right : Integer) return Integer;

-- If you're dealing with new types the compiler can.
Type ShortEx is new Integer range 0..128;
Function "+"(Left, Right : Natural) return Natural;
Function "+"(Left, Right : ShortEx) return ShortEx;

-----------------------
--  Strings Example  --
-----------------------

     Package Tax is
         Type Tax_ID(<>) is private;

         -- SSN format: ###-##-####
         Subtype Social_Security_Number is String(1..11)
         with Dynamic_Predicate =>
           (for all Index in Social_Security_Number'Range =>
              (case Index is
                 when 4|7 => Social_Security_Number(Index) = '-',
               when others => Social_Security_Number(Index) in '0'..'9'
              )
           );

         -- EIN format: ##-#######
         Subtype EIN is String(1..10)
         with Dynamic_Predicate =>
           (for all Index in Social_Security_Number'Range =>
              (case Index is
                 when 3 => EIN(Index) = '-',
               when others => EIN(Index) in '0'..'9'
              )
           );

     Private
         Type Tax_ID is new String
           with Type_Invariant =>
             String(Tax_ID) in EIN or
             String(Tax_ID) in Social_Security_Number or
             raise Constraint_Error;
     End Tax;



      parent reply	other threads:[~2014-07-26  3:34 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-25 16:40 Effective use of derived types Victor Porton
2014-07-25 17:38 ` Dmitry A. Kazakov
2014-07-25 18:00   ` Victor Porton
2014-07-25 18:40     ` Dan'l Miller
2014-07-25 19:33       ` Dmitry A. Kazakov
2014-07-26  1:33         ` Dan'l Miller
2014-07-26  3:34 ` Shark8 [this message]
replies disabled

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