comp.lang.ada
 help / color / mirror / Atom feed
From: tmoran@acm.org
Subject: Re: Declaring subtypes
Date: Tue, 09 Dec 2003 18:47:29 GMT
Date: 2003-12-09T18:47:29+00:00	[thread overview]
Message-ID: <ZMoBb.347213$275.1144434@attbi_s53> (raw)
In-Reply-To: 95476e79.0312090710.68a031a4@posting.google.com

> ... wondering if declaring subtypes of String for common database
> fields was overkill.
> ...
> subtype Address_Line is String(1..60);
> subtype City is String(1..40);
> ...
> I understand that Strong typing is one of the strengths of Ada but I
> just don't know how far to go with it.
    Strong typing has to do with the compiler warning you when you try
to mix type Apples with type Oranges.  Subtypes merely specify a
constraint.  So Address_Line and City are both of type String and
   Home : Address_Line;
   Home_City   : City;
   ...
   Ada.Text_IO.Put_Line(Home & ", " & Home_City);
   Home(1 .. 40) := Home_City;
does not violate strong typing and is OK (as far as the compiler can
tell).  If you have a database where City is always 40 characters (blank
padded, perhaps), then
  subtype City is String(1..40);
is eminently reasonable.  Ditto for Address_Line etc.  If City is of
varying lengths though, you might want to use the facilities of
Ada.Strings.Bounded or Ada.Strings.Unbounded instead.
  If you want to use strong typing so the compiler will warn you about
mixed types then you need
    type Address_Line is new String(1..60);
    type City is new String(1..40);
If you do that, then
   Home(1 .. 40) := Home_City;
will generate an error message from the compiler.  That may be useful
in catching coding errors.  OTOH,
   Ada.Text_IO.Put_Line(Home & ", " & Home_City);
will also generate an error message, which is probably not so helpful.
To accomplish the same thing you would have to write something like
   Ada.Text_IO.Put_Line(String(Home) & ", " & String(Home_City));
Whether the error checking benefits outweigh the verbosity of the
extra type conversions depends of course on the characteristics of
your particular application.  In this particular example, for instance,
changing the variable "Home" to "Home_Street" would probably lessen
errors like
   Home_Street(1 .. 40) := Home_City;
so it probably wouldn't be worthwhile to go for full separate string types.



  parent reply	other threads:[~2003-12-09 18:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-12-09 15:10 Declaring subtypes Kevin Hostelley
2003-12-09 15:52 ` Georg Bauhaus
2003-12-09 17:01 ` Martin Krischik
2003-12-09 18:47 ` tmoran [this message]
2003-12-10 13:48 ` Marin David Condic
replies disabled

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