comp.lang.ada
 help / color / mirror / Atom feed
* Noob question: universal_integer type
@ 2010-05-21 23:15 Duke Normandin
  2010-05-21 23:33 ` Yannick Duchêne (Hibou57)
  2010-05-21 23:37 ` Jeffrey R. Carter
  0 siblings, 2 replies; 12+ messages in thread
From: Duke Normandin @ 2010-05-21 23:15 UTC (permalink / raw)


Coronado's Ada tutorial - Chapt3

[quote]
The constant 17 is of a very special type defined by Ada as type
"universal_integer" which can be combined with any of the integer types
without specific conversion. 
[/quote]

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?
-- 
Duke
*** Tolerance becomes a crime, when applied to evil [Thomas Mann] *** 




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

* Re: Noob question: universal_integer type
  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
  1 sibling, 1 reply; 12+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2010-05-21 23:33 UTC (permalink / raw)


Le Sat, 22 May 2010 01:15:19 +0200, Duke Normandin <dukeofperl@ml1.net> a  
écrit:

> Coronado's Ada tutorial - Chapt3
>
> [quote]
> The constant 17 is of a very special type defined by Ada as type
> "universal_integer" which can be combined with any of the integer types
> without specific conversion.
> [/quote]
>
> 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?
On this subject, you may like to read a previous thread named “Integer  
questia”.
Here is a Google Group link for conveniance:
http://groups.google.com/group/comp.lang.ada/browse_thread/thread/d9d2bccce5d4fc93#

The key, is that universal integer, is a type like other types you may  
defined. Every type integer-like type can be converted to and from  
universal integer.

So, yes, the compiler will not complain, because if you have

    My_Constant : constant := 1; -- Named number, that is here, universal  
integer.
    type My_Integer_Type is range 1 .. 9;
    My_Entity : My_Integer_Type := My_Constant;

the compiler will not see a contradiction, it will not see the declaration  
of a given type which is initialized with a value of a different type, it  
will see a literal, which it will automatically convert to the target type.

-- 
There is even better than a pragma Assert: a SPARK --# check.



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

* Re: Noob question: universal_integer type
  2010-05-21 23:33 ` Yannick Duchêne (Hibou57)
@ 2010-05-21 23:34   ` Yannick Duchêne (Hibou57)
  0 siblings, 0 replies; 12+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2010-05-21 23:34 UTC (permalink / raw)


Le Sat, 22 May 2010 01:33:02 +0200, Yannick Duchêne (Hibou57)  
<yannick_duchene@yahoo.fr> a écrit:
> The key, is that universal integer, is a type like other types you may
Sorry, a word is missing. Please, read “universal integer, is *not* a type  
like other types”.
With apologizes.

-- 
There is even better than a pragma Assert: a SPARK --# check.



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

* Re: Noob question: universal_integer type
  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:37 ` Jeffrey R. Carter
  2010-05-22  2:04   ` Duke Normandin
  2010-05-22  7:16   ` Niklas Holsti
  1 sibling, 2 replies; 12+ messages in thread
From: Jeffrey R. Carter @ 2010-05-21 23:37 UTC (permalink / raw)


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.

-- 
Jeff Carter
"C's solution to this [variable-sized array parameters] has real
problems, and people who are complaining about safety definitely
have a point."
Dennis Ritchie
25



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

* Re: Noob question: universal_integer type
  2010-05-21 23:37 ` Jeffrey R. Carter
@ 2010-05-22  2:04   ` Duke Normandin
  2010-05-22  7:16   ` Niklas Holsti
  1 sibling, 0 replies; 12+ messages in thread
From: Duke Normandin @ 2010-05-22  2:04 UTC (permalink / raw)


On 2010-05-21, Jeffrey R. Carter <spam.jrcarter.not@spam.acm.org> 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.
>

OK! I got it...
-- 
Duke
*** Tolerance becomes a crime, when applied to evil [Thomas Mann] *** 




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

* Re: Noob question: universal_integer type
  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
  1 sibling, 1 reply; 12+ messages in thread
From: Niklas Holsti @ 2010-05-22  7:16 UTC (permalink / raw)


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



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

* Re: Noob question: universal_integer type
  2010-05-22  7:16   ` Niklas Holsti
@ 2010-05-22 13:04     ` Duke Normandin
  2010-05-22 13:47       ` Dmitry A. Kazakov
  2010-05-22 20:08       ` Yannick Duchêne (Hibou57)
  0 siblings, 2 replies; 12+ messages in thread
From: Duke Normandin @ 2010-05-22 13:04 UTC (permalink / raw)


On 2010-05-22, Niklas Holsti <niklas.holsti@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] *** 




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

* Re: Noob question: universal_integer type
  2010-05-22 13:04     ` Duke Normandin
@ 2010-05-22 13:47       ` Dmitry A. Kazakov
  2010-05-22 14:51         ` Duke Normandin
  2010-05-22 20:08       ` Yannick Duchêne (Hibou57)
  1 sibling, 1 reply; 12+ messages in thread
From: Dmitry A. Kazakov @ 2010-05-22 13:47 UTC (permalink / raw)


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



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

* Re: Noob question: universal_integer type
  2010-05-22 13:47       ` Dmitry A. Kazakov
@ 2010-05-22 14:51         ` Duke Normandin
  0 siblings, 0 replies; 12+ messages in thread
From: Duke Normandin @ 2010-05-22 14:51 UTC (permalink / raw)


On 2010-05-22, Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> wrote:
> 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.
>

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] *** 




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

* Re: Noob question: universal_integer type
  2010-05-22 13:04     ` Duke Normandin
  2010-05-22 13:47       ` Dmitry A. Kazakov
@ 2010-05-22 20:08       ` Yannick Duchêne (Hibou57)
  2010-05-23  2:28         ` Duke Normandin
  1 sibling, 1 reply; 12+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2010-05-22 20:08 UTC (permalink / raw)


Le Sat, 22 May 2010 15:04:03 +0200, Duke Normandin <dukeofperl@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.



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

* Re: Noob question: universal_integer type
  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)
  0 siblings, 1 reply; 12+ messages in thread
From: Duke Normandin @ 2010-05-23  2:28 UTC (permalink / raw)


On 2010-05-22, Yannick Duch�ne <yannick_duchene@yahoo.fr> wrote:
> Le Sat, 22 May 2010 15:04:03 +0200, Duke Normandin <dukeofperl@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 ?
>

http://www.infres.enst.fr/~pautet/Ada95/a95list.htm
-- 
Duke
*** Tolerance becomes a crime, when applied to evil [Thomas Mann] *** 




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

* Re: Noob question: universal_integer type
  2010-05-23  2:28         ` Duke Normandin
@ 2010-05-23  2:36           ` Yannick Duchêne (Hibou57)
  0 siblings, 0 replies; 12+ messages in thread
From: Yannick Duchêne (Hibou57) @ 2010-05-23  2:36 UTC (permalink / raw)


Le Sun, 23 May 2010 04:28:35 +0200, Duke Normandin <dukeofperl@ml1.net> a  
écrit:
>> What is the Coronado tutorial you oftenly refer to ?
>>
>
> http://www.infres.enst.fr/~pautet/Ada95/a95list.htm
Oh thanks, did not knew this one, will suggest it some future days when I  
will feel it would be good to do so.

Funny, on this page, there is a link named “How to Remit Payment For this  
Tutorial!”, but this led me to an error 404 page. Will have to tell the  
author about it.

-- 
There is even better than a pragma Assert: a SPARK --# check.



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

end of thread, other threads:[~2010-05-23  2:36 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
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)

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