comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Noob question: universal_integer type
Date: Sat, 22 May 2010 15:47:28 +0200
Date: 2010-05-22T15:47:28+02:00	[thread overview]
Message-ID: <lojvpm92tk66.jhp2xd8ne6ag$.dlg@40tude.net> (raw)
In-Reply-To: 7DQJn.4466$Z6.1110@edtnps82

On Sat, 22 May 2010 13:04:03 GMT, Duke Normandin wrote:

> On 2010-05-22, Niklas Holsti <niklas.holsti@tidorum.invalid> wrote:

>> Perhaps it is also worth mentioning that Ada does have a way of 
>> explicitly indicating the type to be chosen for a literal, by 
>> "qualifying" it with a type name. This can be necessary to resolve 
>> overloaded operation names. For example, assume that you define two 
>> integer types:
>>
>>     type Apples is range 0 .. 20;
>>     type Ants is range 0 .. 1_000_000;
>>
>> and then define a procedure "Eat" for each type, with different content 
>> for eating apples and for eating ants:
>>
>>     procedure Eat (Items : Apples) ... end Eat;
>>
>>     procedure Eat (Items : Ants) ... end Eat;
>>
>> A call of Eat with a literal parameter, for example Eat (17), is then 
>> ambiguous (and the compiler will tell you so). To show if you are eating 
>> apples or ants, you qualify the literal with the type name and an 
>> apostrophe, as in
>>
>>     Eat (Apples'(17));
>>
>> for eating 17 apples, or
>>
>>     Eat (Ants'(17))
>>
>> for eating 17 ants. Since the type of the parameter is now explicit, the 
>> compiler knows which procedure Eat is to be called.
> 
> I love it! and the compiler would choke with an "out of bounds" exception if
> you called Eat (Apples'(25)); - if I understand the Coronado tutorial
> correctly. Thanks for the input...

Yes, the Constraint_Error exception. But note, this is OK:

   Eat (Apples'(25 - 10));

This is another reason behind universal types. You can perform computations
on them ignoring constraints of the particular type.

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



  reply	other threads:[~2010-05-22 13:47 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-05-21 23:15 Noob question: universal_integer type Duke Normandin
2010-05-21 23:33 ` Yannick Duchêne (Hibou57)
2010-05-21 23:34   ` Yannick Duchêne (Hibou57)
2010-05-21 23:37 ` Jeffrey R. Carter
2010-05-22  2:04   ` Duke Normandin
2010-05-22  7:16   ` Niklas Holsti
2010-05-22 13:04     ` Duke Normandin
2010-05-22 13:47       ` Dmitry A. Kazakov [this message]
2010-05-22 14:51         ` Duke Normandin
2010-05-22 20:08       ` Yannick Duchêne (Hibou57)
2010-05-23  2:28         ` Duke Normandin
2010-05-23  2:36           ` Yannick Duchêne (Hibou57)
replies disabled

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