comp.lang.ada
 help / color / mirror / Atom feed
From: "Yannick Duchêne (Hibou57)" <yannick_duchene@yahoo.fr>
Subject: Re: Q: type ... is new String
Date: Mon, 04 Jun 2012 07:58:06 +0200
Date: 2012-06-04T07:58:06+02:00	[thread overview]
Message-ID: <op.wfc6u4g4ule2fv@douda-yannick> (raw)
In-Reply-To: 336f4e4b-16da-4fa8-9b7a-7e2dd3a48018@w24g2000vby.googlegroups.com

Le Thu, 31 May 2012 09:37:22 +0200, Maciej Sobczak  
<see.my.homepage@gmail.com> a écrit:

> On 30 Maj, 18:33, tmo...@acm.org wrote:
>> Any comments on the effectiveness of using multiple types for different
>> categories of strings, rather than "String" all the time?
>
> Interestingly, this is a bit different from defining multiple number
> types instead of using standard types all the time.
> The difference is that number types carry bounds as additional
> information and that can be comfortably used to later define arrays,
> loop ranges, etc.
> Strings don't have this capability (unless, of course, you define
> constrained String (A .. B) types)

There is nothing like a range with text type, but there is still provision  
for contracts (pre/post in Ada 2012). An invariant for a Path_Type would  
be as much useful as a range for a numeric type would be.

Talking about range and this particular case, prior to Ada 2012, there was  
no way to narrow the range for a derive type of an array type, without  
freezing its bounds. It's now possible with Ada 2012.


Prior to Ada 2012:

    Minimum_Length : constant := <platform dependent>;
    Maximum_Length : constant := <platform dependent>;

    subtype Index_Type is Natural range Minimum_Length .. Maximum_Length;

    type Path_Type is new String (Index_Type);
    -- Unfortunately, this freeze the lower and upper bound, and the
    -- declaration below fails.

    XYZ_Location : Path_Type := "..................."; -- Will fail.


Since Ada 2012:

    Minimum_Length : constant := <platform dependent>;
    Maximum_Length : constant := <platform dependent>;

    subtype Index_Type is Natural range Minimum_Length .. Maximum_Length;

    subtype Path_Type is String
       with Static_Predicate => Path_Type'Length in Index_Type;
       -- You may add additional contracts, specific to file paths,
       -- like conformance with a format.

    XYZ_Location : Path_Type := "..................."; -- OK.

-- 
“Syntactic sugar causes cancer of the semi-colons.” [1]
“Structured Programming supports the law of the excluded muddle.” [1]
[1]: Epigrams on Programming — Alan J. — P. Yale University



  reply	other threads:[~2012-06-04  5:58 UTC|newest]

Thread overview: 41+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-05-30 16:33 Q: type ... is new String tmoran
2012-05-30 17:04 ` Dmitry A. Kazakov
2012-05-31  7:37 ` Maciej Sobczak
2012-06-04  5:58   ` Yannick Duchêne (Hibou57) [this message]
2012-06-04  6:30     ` J-P. Rosen
2012-06-04  7:48       ` Yannick Duchêne (Hibou57)
2012-06-04  8:03         ` Dmitry A. Kazakov
2012-06-04  8:14           ` Yannick Duchêne (Hibou57)
2012-06-04  9:09             ` Dmitry A. Kazakov
2012-06-04 10:35         ` J-P. Rosen
2012-06-04  5:43 ` Yannick Duchêne (Hibou57)
2012-06-04 11:39 ` Brian Drummond
2012-06-04 13:36   ` Maciej Sobczak
2012-06-04 14:58     ` Georg Bauhaus
2012-06-04 15:14       ` Dmitry A. Kazakov
2012-06-04 16:06         ` Georg Bauhaus
2012-06-04 17:05           ` Dmitry A. Kazakov
2012-06-04 20:28             ` Yannick Duchêne (Hibou57)
2012-06-04 20:56             ` Georg Bauhaus
2012-06-05  7:32               ` Dmitry A. Kazakov
2012-06-05  8:40                 ` Georg Bauhaus
2012-06-05  9:06                   ` Dmitry A. Kazakov
2012-06-05 12:20                     ` Georg Bauhaus
2012-06-05 13:14                       ` Dmitry A. Kazakov
2012-06-06  4:09                       ` Shark8
2012-06-06  8:52                         ` Georg Bauhaus
2012-06-06 23:56                       ` Randy Brukardt
2012-06-07 11:15                         ` Georg Bauhaus
2012-06-06 23:51                 ` Randy Brukardt
2012-06-04 20:33         ` Yannick Duchêne (Hibou57)
2012-06-04 21:27           ` Georg Bauhaus
     [not found]             ` <m9kqs7hgii13e220b1phm46n43d92tu1pj@invalid.netcom.com>
2012-06-05  6:15               ` Georg Bauhaus
2012-06-05  6:36                 ` Yannick Duchêne (Hibou57)
2012-06-06  4:14               ` Shark8
2012-06-07  0:01             ` Randy Brukardt
2012-06-07  0:20               ` Adam Beneschan
2012-06-05 12:05     ` Brian Drummond
2012-06-05 14:43       ` Yannick Duchêne (Hibou57)
2012-06-04 15:54   ` Shark8
2012-06-04 22:01     ` Jeffrey Carter
2012-06-05 12:10       ` Brian Drummond
replies disabled

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