From: Niklas Holsti on
Jeffrey R. Carter wrote:
> Duke Normandin wrote:
>>
>> Let me get this right... if I use an undeclared integer in an expression,
>> Ada will "deem it" to be a "universal_integer" and not choke at
>> compile-time?
>
> I don't know, and I've been using Ada since 1984. What is "an undeclared
> integer"?
>
> 17 is an integer literal; all integer literals are universal_integer. 17
> is not an undeclared integer.
>
> What the tutorial is trying to get across is that Ada, unlike some
> languages, does not have typed numeric literals (see also
> universal_real). You might encounter a language in which 10 is a literal
> of type int and 10L a literal of long int, for example. In Ada, all
> integer literals are universal_integer, and implicitly converted to
> specific integer types as required.
>
> Partly this makes life easier: you can change the type of a variable and
> not have to change all the literals used with that variable; and partly
> it's pretty much needed in a language that lets you define your own
> numeric types.

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.

--
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
. @ .
From: Duke Normandin on
On 2010-05-22, Niklas Holsti <niklas.holsti(a)tidorum.invalid> wrote:
> Jeffrey R. Carter wrote:
>> Duke Normandin wrote:
>>>
>>> Let me get this right... if I use an undeclared integer in an expression,
>>> Ada will "deem it" to be a "universal_integer" and not choke at
>>> compile-time?
>>
>> I don't know, and I've been using Ada since 1984. What is "an undeclared
>> integer"?
>>
>> 17 is an integer literal; all integer literals are universal_integer. 17
>> is not an undeclared integer.
>>
>> What the tutorial is trying to get across is that Ada, unlike some
>> languages, does not have typed numeric literals (see also
>> universal_real). You might encounter a language in which 10 is a literal
>> of type int and 10L a literal of long int, for example. In Ada, all
>> integer literals are universal_integer, and implicitly converted to
>> specific integer types as required.
>>
>> Partly this makes life easier: you can change the type of a variable and
>> not have to change all the literals used with that variable; and partly
>> it's pretty much needed in a language that lets you define your own
>> numeric types.
>
> 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...
--
Duke Normandin
*** Tolerance becomes a crime, when applied to evil [Thomas Mann] ***

From: Dmitry A. Kazakov on
On Sat, 22 May 2010 13:04:03 GMT, Duke Normandin wrote:

> On 2010-05-22, Niklas Holsti <niklas.holsti(a)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
From: Duke Normandin on
On 2010-05-22, Dmitry A. Kazakov <mailbox(a)dmitry-kazakov.de> wrote:
> On Sat, 22 May 2010 13:04:03 GMT, Duke Normandin wrote:
>
>> On 2010-05-22, Niklas Holsti <niklas.holsti(a)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.
>

Cool!

[side-bar]
All is well with Ada and I - I just discovered Emacs ada-mode, and Stephen
Leake's Emacs ada-mode homepage. Now I'm somewhat on familiar territory.
With Emacs and Ada, we should be able to "kick butt" ;)
--
Duke
*** Tolerance becomes a crime, when applied to evil [Thomas Mann] ***

From: Yannick DuchĂȘne (Hibou57) on
Le Sat, 22 May 2010 15:04:03 +0200, Duke Normandin <dukeofperl(a)ml1.net> a
Ă©crit:
> 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...
What is the Coronado tutorial you oftenly refer to ?

--
There is even better than a pragma Assert: a SPARK --# check.
First  |  Prev  |  Next  |  Last
Pages: 1 2 3
Prev: Ada-bindings for 0mq.
Next: Ada Debian policy plea