comp.lang.ada
 help / color / mirror / Atom feed
* type and subtype
@ 2005-07-24 14:24 Douglas Chong
  2005-07-24 14:39 ` Dmitry A. Kazakov
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Douglas Chong @ 2005-07-24 14:24 UTC (permalink / raw)


I'm a newbie in Ada 95.
Can some one tell me what is the different between type and subtype ?
Both are look the same for me. Thanks




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

* Re: type and subtype
  2005-07-24 14:24 type and subtype Douglas Chong
@ 2005-07-24 14:39 ` Dmitry A. Kazakov
  2005-07-25 17:07   ` Martin Krischik
  2005-07-24 16:20 ` Ludovic Brenta
  2005-07-25 17:02 ` Martin Krischik
  2 siblings, 1 reply; 10+ messages in thread
From: Dmitry A. Kazakov @ 2005-07-24 14:39 UTC (permalink / raw)


On 24 Jul 2005 07:24:38 -0700, Douglas Chong wrote:

> I'm a newbie in Ada 95.
> Can some one tell me what is the different between type and subtype ?
> Both are look the same for me. Thanks

"A subtype of a given type is a combination of the type, a constraint on
values of the type, and certain attributes specific to the subtype. The
given type is called the type of the subtype. Similarly, the associated
constraint is called the constraint of the subtype. The set of values of a
subtype consists of the values of its type that satisfy its constraint.
Such values belong to the subtype."
       -- ARM 3.2(8)

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: type and subtype
  2005-07-24 14:24 type and subtype Douglas Chong
  2005-07-24 14:39 ` Dmitry A. Kazakov
@ 2005-07-24 16:20 ` Ludovic Brenta
  2005-07-24 16:40   ` Jeffrey Carter
  2005-07-25 17:02 ` Martin Krischik
  2 siblings, 1 reply; 10+ messages in thread
From: Ludovic Brenta @ 2005-07-24 16:20 UTC (permalink / raw)


"Douglas Chong" <ohiocky@gmail.com> writes:
> I'm a newbie in Ada 95.
> Can some one tell me what is the different between type and subtype ?
> Both are look the same for me. Thanks

Two types are incompatible with one another.  One can convert between
different scalar types using an explicit type conversion.

Two subtypes of the same type are compatible with one another.  One
can convert between them using an implicit type conversion (i.e. no
special syntax required).

Both implicit and explicit type conversions involve run-time checks.

HTH





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

* Re: type and subtype
  2005-07-24 16:20 ` Ludovic Brenta
@ 2005-07-24 16:40   ` Jeffrey Carter
  2005-07-25  8:23     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 10+ messages in thread
From: Jeffrey Carter @ 2005-07-24 16:40 UTC (permalink / raw)


Ludovic Brenta wrote:
> 
> Two subtypes of the same type are compatible with one another.  One
> can convert between them using an implicit type conversion (i.e. no
> special syntax required).

There is no type conversion between subtypes of the same type, since 
they are the same type. There are checks that the value of the source 
matches the constraints of the destination.

Consider:

type Year_Number is range 1901 .. 2099;
type Month_Number is range 1 .. 12;

Y : Year_Number := 2000;
M : Month_Number := 6;

procedure P (Y : in Year_Number);

...

Y := M; -- illegal, different types
P (Y => Y + M); -- illegal, no such "+"

and then:

subtype Year_Number is Integer range 1901 .. 2099;
subtype Month_Number is Integer range 1 .. 12;

Y : Year_Number := 2000;
M : Month_Number := 6;

procedure P (Y : in Year_Number);

...

Y := M; -- legal, same type
P (Y => Y + M); -- legal, "+" (Left, Right : Integer) return Integer;
                 -- Y + M in Year_Number

M := 12;
M := 2 * M - M; -- legal, operations evaluated for type (Integer)
                 -- result in Month_Number

-- 
Jeff Carter
"I like it when the support group complains that they have
insufficient data on mean time to repair bugs in Ada software."
Robert I. Eachus
91



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

* Re: type and subtype
  2005-07-24 16:40   ` Jeffrey Carter
@ 2005-07-25  8:23     ` Dmitry A. Kazakov
  2005-07-25 15:41       ` Jeffrey Carter
  0 siblings, 1 reply; 10+ messages in thread
From: Dmitry A. Kazakov @ 2005-07-25  8:23 UTC (permalink / raw)


On Sun, 24 Jul 2005 16:40:26 GMT, Jeffrey Carter wrote:

> Ludovic Brenta wrote:
>> 
>> Two subtypes of the same type are compatible with one another.  One
>> can convert between them using an implicit type conversion (i.e. no
>> special syntax required).
> 
> There is no type conversion between subtypes of the same type, since 
> they are the same type. There are checks that the value of the source 
> matches the constraints of the destination.

This sounds much like hairsplitting. However it is an important issue for
the future.

I think that Ludovic's view is more consistent: checks are just a part of
the conversion which otherwise is an identity function, because *presently*
the representation is required to be same. But representation is an
implementation detail and thus cannot count. Moreover it is very desirable
to allow subtypes having different representations. For example: statically
constrained subtypes with removed constant discriminants from their values;
T'Class constrained to some specific type S could remove the type tag from
its objects, etc. Such subtypes would require more-than-checks conversions.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

* Re: type and subtype
  2005-07-25  8:23     ` Dmitry A. Kazakov
@ 2005-07-25 15:41       ` Jeffrey Carter
  2005-07-25 21:59         ` Ludovic Brenta
  2005-07-26  8:21         ` Dmitry A. Kazakov
  0 siblings, 2 replies; 10+ messages in thread
From: Jeffrey Carter @ 2005-07-25 15:41 UTC (permalink / raw)


[-- Attachment #1: Type: text/plain, Size: 1090 bytes --]

Dmitry A. Kazakov wrote:
> 
> I think that Ludovic's view is more consistent: checks are just a part of
> the conversion which otherwise is an identity function, because *presently*
> the representation is required to be same. But representation is an
> implementation detail and thus cannot count. Moreover it is very desirable
> to allow subtypes having different representations.

Try this with your favorite compiler:

Ada.Text_IO.Put_Line
    (Item => "Integer'Size =" & Integer'Image (Integer'Size) ):
Ada.Text_IO.Put_Line
    (Item => "Natural'Size =" & Integer'Image (Natural'Size) ):

You're likely to get something like

Integer'Size = 32
Natural'Size = 31

because representations of subtypes are not required to be the same.

We can also consider

type Rec (Length : Positive) is record
    V : String (1 .. Length);
end record;

subtype Small is Rec (Length =>     1);
subtype Big   is Rec (length => 1_000);

where Small and Big have significantly different representations. Then 
there are subtypes of variant record types with discriminants that 
select different variants.

[-- Attachment #2: spam.vcf --]
[-- Type: text/x-vcard, Size: 177 bytes --]

begin:vcard
fn:Jeffrey Carter
n:;Jeffrey Carter
email;internet:jeffrey_r_carter-nr [commercial-at] raytheon [period | full stop] com
x-mozilla-html:FALSE
version:2.1
end:vcard


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

* Re: type and subtype
  2005-07-24 14:24 type and subtype Douglas Chong
  2005-07-24 14:39 ` Dmitry A. Kazakov
  2005-07-24 16:20 ` Ludovic Brenta
@ 2005-07-25 17:02 ` Martin Krischik
  2 siblings, 0 replies; 10+ messages in thread
From: Martin Krischik @ 2005-07-25 17:02 UTC (permalink / raw)


Douglas Chong wrote:

> I'm a newbie in Ada 95.
> Can some one tell me what is the different between type and subtype ?
> Both are look the same for me. Thanks

http://en.wikibooks.org/wiki/Programming:Ada:Subtypes

And yes: google does craws wikibooks.

Martin

-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com




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

* Re: type and subtype
  2005-07-24 14:39 ` Dmitry A. Kazakov
@ 2005-07-25 17:07   ` Martin Krischik
  0 siblings, 0 replies; 10+ messages in thread
From: Martin Krischik @ 2005-07-25 17:07 UTC (permalink / raw)


Dmitry A. Kazakov wrote:

> On 24 Jul 2005 07:24:38 -0700, Douglas Chong wrote:
> 
>> I'm a newbie in Ada 95.
>> Can some one tell me what is the different between type and subtype ?
>> Both are look the same for me. Thanks
> 
> "A subtype of a given type is a combination of the type, a constraint on
> values of the type, and certain attributes specific to the subtype. The
> given type is called the type of the subtype. Similarly, the associated
> constraint is called the constraint of the subtype. The set of values of a
> subtype consists of the values of its type that satisfy its constraint.
> Such values belong to the subtype."

Please the ARM  is not the easiest reading, and do we have easier text one
can point to:

http://en.wikibooks.org/wiki/Programming:Ada:Subtypes#Type_vs._Subtype

Martin

-- 
mailto://krischik@users.sourceforge.net
Ada programming at: http://ada.krischik.com




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

* Re: type and subtype
  2005-07-25 15:41       ` Jeffrey Carter
@ 2005-07-25 21:59         ` Ludovic Brenta
  2005-07-26  8:21         ` Dmitry A. Kazakov
  1 sibling, 0 replies; 10+ messages in thread
From: Ludovic Brenta @ 2005-07-25 21:59 UTC (permalink / raw)


Jeffrey Carter <spam@spam.com> writes:
> Dmitry A. Kazakov wrote:
> Integer'Size = 32
> Natural'Size = 31
>
> because representations of subtypes are not required to be the same.

And this all confirms my initial point: conversions between subtypes
of the same type involve run-time checks, which may raise
Constraint_Error.  Additionally, in some (but not all) cases, implicit
conversions may involve changes in representation, i.e. an actual
conversion which is not a no-op.

-- 
Ludovic Brenta.



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

* Re: type and subtype
  2005-07-25 15:41       ` Jeffrey Carter
  2005-07-25 21:59         ` Ludovic Brenta
@ 2005-07-26  8:21         ` Dmitry A. Kazakov
  1 sibling, 0 replies; 10+ messages in thread
From: Dmitry A. Kazakov @ 2005-07-26  8:21 UTC (permalink / raw)


On Mon, 25 Jul 2005 08:41:48 -0700, Jeffrey Carter wrote:

> Dmitry A. Kazakov wrote:
>> 
>> I think that Ludovic's view is more consistent: checks are just a part of
>> the conversion which otherwise is an identity function, because *presently*
>> the representation is required to be same. But representation is an
>> implementation detail and thus cannot count. Moreover it is very desirable
>> to allow subtypes having different representations.
> 
> because representations of subtypes are not required to be the same.

Depends on the definition, but I better agree with this. After all there
are other examples like String vs. String (1..20).

So, if representations aren't same then a conversion is required. Also
Ludovic was right.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



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

end of thread, other threads:[~2005-07-26  8:21 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2005-07-24 14:24 type and subtype Douglas Chong
2005-07-24 14:39 ` Dmitry A. Kazakov
2005-07-25 17:07   ` Martin Krischik
2005-07-24 16:20 ` Ludovic Brenta
2005-07-24 16:40   ` Jeffrey Carter
2005-07-25  8:23     ` Dmitry A. Kazakov
2005-07-25 15:41       ` Jeffrey Carter
2005-07-25 21:59         ` Ludovic Brenta
2005-07-26  8:21         ` Dmitry A. Kazakov
2005-07-25 17:02 ` Martin Krischik

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