One Simple Type declaration Question 
Author Message
 One Simple Type declaration Question

Why cant I declare something like :

TYPE Months_With_31_Days IS (1, 3, 5, 7, 8, 10, 12);

and then test like

IF Current_Month IN Months_With_31_Days THEN

I get compilation error Identifier expected on the 1,
ie the first value in my type.

maa

P.S. Please cc: me too if/when answering. Thanks!

__________________________________________________
Do You Yahoo!?
Send instant messages with Yahoo! Messenger.
http://www.*-*-*.com/



Fri, 06 Dec 2002 03:00:00 GMT  
 One Simple Type declaration Question


Quote:
> Why cant I declare something like :

> TYPE Months_With_31_Days IS (1, 3, 5, 7, 8, 10, 12);

Because integer literals are not enumeration_literal_specifications.

See the Ada Reference Manual, section 3.5.1 for clarification
and elaboration on the distinction.

Quote:

> and then test like

> IF Current_Month IN Months_With_31_Days THEN

> I get compilation error Identifier expected on the 1,
> ie the first value in my type.

> maa

> P.S. Please cc: me too if/when answering. Thanks!

> __________________________________________________
> Do You Yahoo!?
> Send instant messages with Yahoo! Messenger.
> http://im.yahoo.com/



Fri, 06 Dec 2002 03:00:00 GMT  
 One Simple Type declaration Question

Quote:

> Why cant I declare something like :

> TYPE Months_With_31_Days IS (1, 3, 5, 7, 8, 10, 12);

> and then test like

> IF Current_Month IN Months_With_31_Days THEN

> I get compilation error Identifier expected on the 1,
> ie the first value in my type.

What type is Current_Month?

What you want is a set type and a set membership function:

with PragmARC.Set_Discrete;
...
   package Month_Set is new PragmARC.Set_Discrete
      (Element => Calendar.Month_Number);
   use Month_Set;

   Months_With_31_Days : constant Set :=
      Make_Set ( (1, 3, 5, 7, 8, 10, 12) );
...
   if Member (Current_Month, Months_With_31_Days) then
...

--
Jeff Carter
"Monsieur Arthur King, who has the brain of a duck, you know."
Monty python & the Holy Grail



Fri, 06 Dec 2002 03:00:00 GMT  
 One Simple Type declaration Question

Quote:

> Why cant I declare something like :

> TYPE Months_With_31_Days IS (1, 3, 5, 7, 8, 10, 12);

> and then test like

> IF Current_Month IN Months_With_31_Days THEN

It looks like you really want a set type, rather than an enumerated
type. For an explanation of how to do simple sets in Ada, go look at
http://www.adapower.com/lang/sets.html. The basic type you want would
look something like:

subtype Day_Of_Month is Natural range 1..31;
type Day_Set is array (Day_Of_Month) of Boolean;

--
T.E.D.


WWW  - http://www.telepath.com/dennison/Ted/TED.html  ICQ  - 10545591



Fri, 06 Dec 2002 03:00:00 GMT  
 One Simple Type declaration Question

Quote:

> Why cant I declare something like :

> TYPE Months_With_31_Days IS (1, 3, 5, 7, 8, 10, 12);

> and then test like

> IF Current_Month IN Months_With_31_Days THEN

> I get compilation error Identifier expected on the 1,
> ie the first value in my type.

the simple answer is

   because the language doesn't support it.

The reason it doesn't support it is because it's not in the
philosophy of Ada to be like that - the designers thought you
could solve this problem yourself without making the language
larger.

Dale



Sat, 07 Dec 2002 03:00:00 GMT  
 One Simple Type declaration Question
I think she would need a set of month numbers, not a set of
day numbers.


Quote:

> > Why cant I declare something like :

> > TYPE Months_With_31_Days IS (1, 3, 5, 7, 8, 10, 12);

> > and then test like

> > IF Current_Month IN Months_With_31_Days THEN

> It looks like you really want a set type, rather than an enumerated
> type. For an explanation of how to do simple sets in Ada, go look at
> http://www.adapower.com/lang/sets.html. The basic type you want would
> look something like:

> subtype Day_Of_Month is Natural range 1..31;
> type Day_Set is array (Day_Of_Month) of Boolean;

> --
> T.E.D.


> WWW  - http://www.telepath.com/dennison/Ted/TED.html  ICQ  - 10545591



Sat, 07 Dec 2002 03:00:00 GMT  
 One Simple Type declaration Question


Quote:
> I think she would need a set of month numbers, not a set of
> day numbers.

Good point. It depends on what exactly she is trying to do (and if its a
homework assignment, perhaps we should stop now and let her figure those
details out). But I'd think multiple sets of months, one set for each
possible amount of days would probably be a better solution to the
problem of finding the number of days in a given month. Then again,
since there are only 12 of them, it would be just about as easy to make
a constant array of Day_Numbers, indexed by a month enumeration. Of
course to be really thourough you have to treat February as a special
case, where the result depends on the year...

--
T.E.D.

http://www.telepath.com/~dennison/Ted/TED.html

Sent via Deja.com http://www.deja.com/
Before you buy.



Sat, 07 Dec 2002 03:00:00 GMT  
 One Simple Type declaration Question

Quote:

> Why cant I declare something like :

> TYPE Months_With_31_Days IS (1, 3, 5, 7, 8, 10, 12);

> and then test like

> IF Current_Month IN Months_With_31_Days THEN

> I get compilation error Identifier expected on the 1,
> ie the first value in my type.

Something more like the following would work:

    type Month_Number is range 1..12;
    Months_With_31_Days : constant array(Month_Number) of Boolean :=
      (1|3|5|7|8|10|12 => True, others => False);

         -- Note: the above array aggregate is not legal as written
         -- in Ada 83, but is legal in Ada 95, thanks to RM95 4.3.3(25)

   ...
    if Months_With_31_Days(Current_Month) then ...

Quote:

> maa

> P.S. Please cc: me too if/when answering. Thanks!

--

Technical Director, Commercial Division, AverStar (formerly Intermetrics)
(http://www.averstar.com/services/IT_consulting.html)  Burlington, MA  USA


Sat, 14 Dec 2002 03:00:00 GMT  
 One Simple Type declaration Question

Quote:

> TYPE Months_With_31_Days IS (1, 3, 5, 7, 8, 10, 12);

Are you convinced this is a type?
It seems to me like a subset of something larger.

Quote:
> IF Current_Month IN Months_With_31_Days THEN

What type would you make Current_Month ? Integer?
How about making month an enumerated type?

procedure Toto is
  type Month is ( January, February, March, April, May ) ;
  type Days_In_Month is range 28..31 ;
  Days_Per_Month : constant array( Month ) of Days_In_Month :=
    ( 31, 28, 31, 30, 31 ) ;
  Current_Month : Month ;
begin

etc.



Tue, 17 Dec 2002 03:00:00 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Novice Ada Programmer Stumped By Simple Type Declaration!

2. too simple a question : forward declaration?

3. newbie: type declaration question

4. Question in french (a simple question, very simple)

5. Office document types - simple question

6. Simple Question: simulation of primitive data types

7. Simple Type Conversion question

8. All file declarations in one file

9. Multiple declarations for one file

10. one-line function declarations & f90

11. group type declaration

12. Type declarations and generic classes in CLOS - HOW??

 

 
Powered by phpBB® Forum Software