Re: Extended 'Succ attribute?



Jérémie Lumbroso wrote:
Hello,

I just started Ada and was wondering if there is a way to make the Succ
attribute of discrete types behave in a cyclic manner (akin to the
modular type):

type T_DAY is (Mon, Tue, Wed, Thu, Fri, Sat, Sun);

...

day := T_DAY'(Sun);
day := T_DAY'Succ(day); -- that this returns 'Mon' instead of
-- a type constraint error

I could of course define my own function to do this, but I wish to know
if there is a way to get Ada to do it itself.

No.

The best you could do would be to declare a modular type, and then
declare Mon, Tue, etc. as constants of that type:

type T_DAY is mod 7;
Mon : constant T_DAY := 0;
Tue : constant T_DAY := 1;
etc.

Then T_DAY'Succ(X) will wrap around. But I don't see any advantage to
doing things this way. I'd
just make it an enumeration literal and define your own Succ function.

-- Adam

.



Relevant Pages

  • Re: Question about OO programming in Ada
    ... when I declare a procedure ... Both the Ada Rationale and the Guide for C/C++ programmers say that you have ... The Ada 95 Reference says that the "all" attribute provides read-write ... Does that help to avoid casting as well? ...
    (comp.lang.ada)
  • Re: advice on package design
    ... > for following scope. ... the compiler would have to do ... the extra verbiage required just to declare X. ... Unfortunately this is also untrue in Ada. ...
    (comp.lang.ada)
  • Re: A novel design of linked lists
    ... Ada does not provide them. ... elements of a double-linked lists as OK, then we are on different pages. ... I don't see why it wouldn't be OK to declare ... this definitely applies to the dopes of String objects. ...
    (comp.lang.ada)
  • Re: A novel design of linked lists
    ... elements of a double-linked lists as OK, then we are on different pages. ... I don't see why it wouldn't be OK to declare ... pointers should be fully transparent in Ada. ... this definitely applies to the dopes of String objects. ...
    (comp.lang.ada)
  • Re: Instantiating private types with discriminants?
    ... Have I got to declare two new packages - one against ... is declared in the same package specification as T (same declarative ... Ada, you can also dispatch with plain variables, as illustrated above. ...
    (comp.lang.ada)