comp.lang.ada
 help / color / mirror / Atom feed
* Declaring subtypes
@ 2003-12-09 15:10 Kevin Hostelley
  2003-12-09 15:52 ` Georg Bauhaus
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Kevin Hostelley @ 2003-12-09 15:10 UTC (permalink / raw)


I am rather new to Ada and I had a design question.  I'm working on a
web based application and I was wondering if declaring subtypes of
String for common database fields was overkill.  For example:

subtype Address_Line is String(1..60);
subtype City is String(1..40);
subtype Zip_Code is String(1..9);


I understand that Strong typing is one of the strengths of Ada but I
just don't know how far to go with it.

Thanks,

mamboking



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Declaring subtypes
  2003-12-09 15:10 Declaring subtypes Kevin Hostelley
@ 2003-12-09 15:52 ` Georg Bauhaus
  2003-12-09 17:01 ` Martin Krischik
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Georg Bauhaus @ 2003-12-09 15:52 UTC (permalink / raw)


Kevin Hostelley <khostelley@yahoo.com> wrote:
: I am rather new to Ada and I had a design question.  I'm working on a
: web based application and I was wondering if declaring subtypes of
: String for common database fields was overkill.  For example:
: 
: subtype Address_Line is String(1..60);
: subtype City is String(1..40);
: subtype Zip_Code is String(1..9);
: 
: 
: I understand that Strong typing is one of the strengths of Ada but I
: just don't know how far to go with it.

If you look back just a few weeks there was a discussions
of this topic in this group. (With comments from Robert Eachus, among
others) An alternative might be to use Bounded_String types.
You could also consider Wide_String or Bounded_Wide_String,
if you have an internationally usable data base.


-- Georg



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Declaring subtypes
  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
  2003-12-10 13:48 ` Marin David Condic
  3 siblings, 0 replies; 5+ messages in thread
From: Martin Krischik @ 2003-12-09 17:01 UTC (permalink / raw)


Kevin Hostelley wrote:

> I am rather new to Ada and I had a design question.  I'm working on a
> web based application and I was wondering if declaring subtypes of
> String for common database fields was overkill.  For example:
> 
> subtype Address_Line is String(1..60);
> subtype City is String(1..40);
> subtype Zip_Code is String(1..9);
> 
> 
> I understand that Strong typing is one of the strengths of Ada but I
> just don't know how far to go with it.

Click here for enlighenment:

http://www.adaic.org/standards/95lrm/html/RM-A-4-4.html
http://www.adaic.org/standards/95lrm/html/RM-A-4-5.html

Strings are goot for interfaces like:

    --
    --  Set Search Text
    --
    procedure Set_Search (
        --  Object itself.
        This   : in out Object;
        --  New sarch text
        Search : in String)
    is
    begin
        This.Search :=  Ada.Strings.Unbounded.To_Unbounded_String (Search);
    end Set_Search;

    --
    --  Get Search Text
    --
    function Get_Search (
        --  Object itself.
        This : in Object)
    return
        String
    is
    begin
        return  Ada.Strings.Unbounded.To_String (This.Search);
    end Get_Search;

But not for actulay storing data.

With regards

Martin

-- 
mailto://krischik@users.sourceforge.net
http://adacl.sourceforge.net




^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Declaring subtypes
  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
  2003-12-10 13:48 ` Marin David Condic
  3 siblings, 0 replies; 5+ messages in thread
From: tmoran @ 2003-12-09 18:47 UTC (permalink / raw)


> ... 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.



^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Declaring subtypes
  2003-12-09 15:10 Declaring subtypes Kevin Hostelley
                   ` (2 preceding siblings ...)
  2003-12-09 18:47 ` tmoran
@ 2003-12-10 13:48 ` Marin David Condic
  3 siblings, 0 replies; 5+ messages in thread
From: Marin David Condic @ 2003-12-10 13:48 UTC (permalink / raw)


Of course, it depends a little on the nature of the database, but 
subtypes are not inappropriate for database fields that you expect to 
have around as more or less permanent things. You might also consider 
bounded strings or unbounded strings as possibly useful ways of storing 
& manipulating things you expect to keep in a database. In any event, 
you could create subtypes from these as well to help you protect against 
mixing things up & to be sure everyone is using common sizes for the fields.

MDC

Kevin Hostelley wrote:
> I am rather new to Ada and I had a design question.  I'm working on a
> web based application and I was wondering if declaring subtypes of
> String for common database fields was overkill.  For example:
> 
> subtype Address_Line is String(1..60);
> subtype City is String(1..40);
> subtype Zip_Code is String(1..9);
> 
> 
> I understand that Strong typing is one of the strengths of Ada but I
> just don't know how far to go with it.
> 
> Thanks,
> 
> mamboking


-- 
======================================================================
Marin David Condic
I work for: http://www.belcan.com/
My project is: http://www.jsf.mil/NSFrames.htm

Send Replies To: m   o   d   c @ a   m   o   g
                    c   n   i       c   .   r

     "Trying is the first step towards failure."
         --  Homer Simpson

======================================================================




^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2003-12-10 13:48 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2003-12-10 13:48 ` Marin David Condic

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