comp.lang.ada
 help / color / mirror / Atom feed
* enumration using integers?
@ 2001-04-19 10:58 Sven Nilsson
  2001-04-19 12:42 ` Marc A. Criley
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Sven Nilsson @ 2001-04-19 10:58 UTC (permalink / raw)


Hello

I have a little problem that I don't want to spend time solving...

If I want to make an enumerated type using integers, like:

   type Distance is (10, 20, 30, 40, 50);

The compiler will complain about missing identifiers. How do I get the
blasted machine to understand that the integers are the identifiers I
want?

Thanks
-Sven



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

* Re: enumration using integers?
  2001-04-19 10:58 enumration using integers? Sven Nilsson
@ 2001-04-19 12:42 ` Marc A. Criley
  2001-04-19 13:02 ` Philip Anderson
  2001-04-19 14:50 ` Ted Dennison
  2 siblings, 0 replies; 10+ messages in thread
From: Marc A. Criley @ 2001-04-19 12:42 UTC (permalink / raw)


Sven Nilsson wrote:
> 
> Hello
> 
> I have a little problem that I don't want to spend time solving...
> 
> If I want to make an enumerated type using integers, like:
> 
>    type Distance is (10, 20, 30, 40, 50);
> 
> The compiler will complain about missing identifiers. How do I get the
> blasted machine to understand that the integers are the identifiers I
> want?
> 

Well, integers are not identifiers, and enumeration literals must be
identifiers, so you cannot create an enumeration type whose literals are
integers.

Given a type definition like:

   type Distance is (Km_10, Km_20, Km_30, Km_40, Km_50);

If you're using values of that type in registers or particular memory
locations, as is commonly done in embedded programming, you can
associate a representation specification with the type:

   for Distance use (Km_10 => 10, Km_20 => 20, Km_30 => 30,
                     Km_40 => 40, Km_50 => 50);

Or you can just use a little lookup table to get the value:

   type Distance_Values is array (Distance) of Natural;

   Dist : constant Distance_Values :=
                  (Km_10 => 10, Km_20 => 20, Km_30 => 30,
                   Km_40 => 40, Km_50 => 50);

Then just look up the value through the table:

   for D in Distance'Range loop
      Process(Dist(D));
   end loop;


Marc A. Criley
Senior Staff Engineer
Quadrus Corporation
www.quadruscorp.com



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

* Re: enumration using integers?
  2001-04-19 10:58 enumration using integers? Sven Nilsson
  2001-04-19 12:42 ` Marc A. Criley
@ 2001-04-19 13:02 ` Philip Anderson
  2001-04-19 14:50 ` Ted Dennison
  2 siblings, 0 replies; 10+ messages in thread
From: Philip Anderson @ 2001-04-19 13:02 UTC (permalink / raw)


Sven Nilsson wrote:
> 
> Hello
> 
> I have a little problem that I don't want to spend time solving...
> 
> If I want to make an enumerated type using integers, like:
> 
>    type Distance is (10, 20, 30, 40, 50);
> 
> The compiler will complain about missing identifiers. How do I get the
> blasted machine to understand that the integers are the identifiers I
> want?

Integers can't be identifiers, since identifiers must begin with a
letter; that's Ada not the compiler.

Use something like "I_10" or simply "Ten".


-- 
hwyl/cheers,
Philip Anderson
Alenia Marconi Systems
Cwmbr�n, Cymru/Wales



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

* Re: enumration using integers?
  2001-04-19 10:58 enumration using integers? Sven Nilsson
  2001-04-19 12:42 ` Marc A. Criley
  2001-04-19 13:02 ` Philip Anderson
@ 2001-04-19 14:50 ` Ted Dennison
  2001-04-20  5:22   ` Sven Nilsson
  2 siblings, 1 reply; 10+ messages in thread
From: Ted Dennison @ 2001-04-19 14:50 UTC (permalink / raw)


In article <3ADEC4E8.954B6830@emw.ericsson.se>, Sven Nilsson says...
>
>If I want to make an enumerated type using integers, like:
>
>   type Distance is (10, 20, 30, 40, 50);
>
>The compiler will complain about missing identifiers. How do I get the
>blasted machine to understand that the integers are the identifiers I
>want?

It understands perfectly what you want. It just won't allow you to do that. :-)

The next obvious question is *why* do you want to do this? What are you trying
to accomplish with an enumeration of integers?

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: enumration using integers?
  2001-04-19 14:50 ` Ted Dennison
@ 2001-04-20  5:22   ` Sven Nilsson
  2001-04-20 13:57     ` Ted Dennison
  2001-04-20 15:52     ` Samuel T. Harris
  0 siblings, 2 replies; 10+ messages in thread
From: Sven Nilsson @ 2001-04-20  5:22 UTC (permalink / raw)


Ted Dennison wrote:
> 
> It understands perfectly what you want. It just won't allow you to do that. :-)

I knew it! The machines are revolting!! Well, this machine is gonna be
playing Britney Spears MP3's the whole day... Take that you ungreatful
piece of ...
> 
> The next obvious question is *why* do you want to do this? What are you trying
> to accomplish with an enumeration of integers?
> 

Right. I want to have a type containing certain integers that I can use
in calculations. Something like this:

  type Distance is (10, 15, 20, 37);
  Dist : Distance := 15;
  Result : Integer := 0;
begin
  Result := Some_Formula * Dist; -- And perhaps a typecast if need be.

I could do that in other ways, for instance as suggested by Marc and
Philip, but that's not really the issue. I wanted to know if I could
magically write something to make my type-construct legal. Which I
probably can't. *sob*

-Sven



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

* Re: enumration using integers?
  2001-04-20  5:22   ` Sven Nilsson
@ 2001-04-20 13:57     ` Ted Dennison
  2001-04-20 14:35       ` Sven Nilsson
  2001-04-20 15:52     ` Samuel T. Harris
  1 sibling, 1 reply; 10+ messages in thread
From: Ted Dennison @ 2001-04-20 13:57 UTC (permalink / raw)


In article <3ADFC797.7D2D537D@emw.ericsson.se>, Sven Nilsson says...
>
>in calculations. Something like this:
>
>  type Distance is (10, 15, 20, 37);
>  Dist : Distance := 15;
>  Result : Integer := 0;


Hmm. I don't see anything in the above that couldn't have been done by making
Distance a sybtype of Integer. Are you saying that you just want an integer type
with "holes" in it, so that 
Dist := 2; 
would raise a Constraint_Error?

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

* Re: enumration using integers?
  2001-04-20 13:57     ` Ted Dennison
@ 2001-04-20 14:35       ` Sven Nilsson
  2001-04-20 15:50         ` Keith Thompson
  2001-04-20 18:18         ` Ted Dennison
  0 siblings, 2 replies; 10+ messages in thread
From: Sven Nilsson @ 2001-04-20 14:35 UTC (permalink / raw)


Ted Dennison wrote:
> Hmm. I don't see anything in the above that couldn't have been done by making
> Distance a sybtype of Integer. Are you saying that you just want an integer type
> with "holes" in it, so that
> Dist := 2;
> would raise a Constraint_Error?

Yepp, that sounds about right. I've been looking for something like that
in Barnes but I couldn't find it... Any clue?

-Sven



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

* Re: enumration using integers?
  2001-04-20 14:35       ` Sven Nilsson
@ 2001-04-20 15:50         ` Keith Thompson
  2001-04-20 18:18         ` Ted Dennison
  1 sibling, 0 replies; 10+ messages in thread
From: Keith Thompson @ 2001-04-20 15:50 UTC (permalink / raw)


Sven Nilsson <emwsvni@emw.ericsson.se> writes:
> Ted Dennison wrote:
> > Hmm. I don't see anything in the above that couldn't have been done
> > by making Distance a sybtype of Integer. Are you saying that you
> > just want an integer type
> > with "holes" in it, so that
> > Dist := 2;
> > would raise a Constraint_Error?
> 
> Yepp, that sounds about right. I've been looking for something like that
> in Barnes but I couldn't find it... Any clue?
> 
> -Sven

Declare your own private type with the desired characteristics.  You
can overload whatever arithmetic operations you need.  You don't get
numeric literals, but you can overload unary "+" to convert from
Integer.

(The language doesn't have *everything* built in.)

-- 
Keith Thompson (The_Other_Keith) kst@cts.com  <http://www.ghoti.net/~kst>
San Diego Supercomputer Center           <*>  <http://www.sdsc.edu/~kst>
Cxiuj via bazo apartenas ni.



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

* Re: enumration using integers?
  2001-04-20  5:22   ` Sven Nilsson
  2001-04-20 13:57     ` Ted Dennison
@ 2001-04-20 15:52     ` Samuel T. Harris
  1 sibling, 0 replies; 10+ messages in thread
From: Samuel T. Harris @ 2001-04-20 15:52 UTC (permalink / raw)


Sven Nilsson wrote:
> 
> Ted Dennison wrote:
> >
> > It understands perfectly what you want. It just won't allow you to do that. :-)
> 
> I knew it! The machines are revolting!! Well, this machine is gonna be
> playing Britney Spears MP3's the whole day... Take that you ungreatful
> piece of ...
> >
> > The next obvious question is *why* do you want to do this? What are you trying
> > to accomplish with an enumeration of integers?
> >
> 
> Right. I want to have a type containing certain integers that I can use
> in calculations. Something like this:
> 
>   type Distance is (10, 15, 20, 37);

The 37 is a real kicker.
It it had been 35 and included 25 and 30, then perhaps ...

type distance is delta 5.0 range 10.0..35.0;
for delta'small use 5.0;

(I believe that is correct off the top of my head).

I've never used a fixed point type to represent an
equi-distance set of integers, but I guess it would
work.

>   Dist : Distance := 15;
>   Result : Integer := 0;
> begin
>   Result := Some_Formula * Dist; -- And perhaps a typecast if need be.
> 
> I could do that in other ways, for instance as suggested by Marc and
> Philip, but that's not really the issue. I wanted to know if I could
> magically write something to make my type-construct legal. Which I
> probably can't. *sob*
> 
> -Sven

-- 
Samuel T. Harris, Principal Engineer
Raytheon, Aerospace Engineering Services
"If you can make it, We can fake it!"



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

* Re: enumration using integers?
  2001-04-20 14:35       ` Sven Nilsson
  2001-04-20 15:50         ` Keith Thompson
@ 2001-04-20 18:18         ` Ted Dennison
  1 sibling, 0 replies; 10+ messages in thread
From: Ted Dennison @ 2001-04-20 18:18 UTC (permalink / raw)


In article <3AE04917.DF97BEA0@emw.ericsson.se>, Sven Nilsson says...
>
>Ted Dennison wrote:
>> Hmm. I don't see anything in the above that couldn't have been done by making
>> Distance a sybtype of Integer. Are you saying that you just want an integer type
>> with "holes" in it, so that
>> Dist := 2;
>> would raise a Constraint_Error?
>
>Yepp, that sounds about right. I've been looking for something like that
>in Barnes but I couldn't find it... Any clue?

Ahhh. We have had that discussion here before. If I recall, the consensus was
that there isn't a way to do that which would generally be worth the effort. If
its really important to you to detect invalid assignments in the middle, then
make it private and write your own "assign" routine that checks. 

You can write your own operation to happen upon every " := ", but that requires
that the type be derived from the type Ada.Controlled, which means that it has
to be a tagged type declared in a library-level package spec. You'd also have to
create your own "+", "-", "*", ect. For a simple integer, again probably not
worth the effort.

---
T.E.D.    homepage   - http://www.telepath.com/dennison/Ted/TED.html
          home email - mailto:dennison@telepath.com



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

end of thread, other threads:[~2001-04-20 18:18 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-04-19 10:58 enumration using integers? Sven Nilsson
2001-04-19 12:42 ` Marc A. Criley
2001-04-19 13:02 ` Philip Anderson
2001-04-19 14:50 ` Ted Dennison
2001-04-20  5:22   ` Sven Nilsson
2001-04-20 13:57     ` Ted Dennison
2001-04-20 14:35       ` Sven Nilsson
2001-04-20 15:50         ` Keith Thompson
2001-04-20 18:18         ` Ted Dennison
2001-04-20 15:52     ` Samuel T. Harris

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