* Extended 'Succ attribute?
@ 2006-11-23 0:55 Jérémie Lumbroso
2006-11-23 2:04 ` Adam Beneschan
2006-11-23 6:38 ` Jeffrey R. Carter
0 siblings, 2 replies; 4+ messages in thread
From: Jérémie Lumbroso @ 2006-11-23 0:55 UTC (permalink / raw)
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.
Regards,
Jérémie
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Extended 'Succ attribute?
2006-11-23 0:55 Extended 'Succ attribute? Jérémie Lumbroso
@ 2006-11-23 2:04 ` Adam Beneschan
2006-11-24 13:02 ` Peter Hermann
2006-11-23 6:38 ` Jeffrey R. Carter
1 sibling, 1 reply; 4+ messages in thread
From: Adam Beneschan @ 2006-11-23 2:04 UTC (permalink / raw)
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
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Extended 'Succ attribute?
2006-11-23 0:55 Extended 'Succ attribute? Jérémie Lumbroso
2006-11-23 2:04 ` Adam Beneschan
@ 2006-11-23 6:38 ` Jeffrey R. Carter
1 sibling, 0 replies; 4+ messages in thread
From: Jeffrey R. Carter @ 2006-11-23 6:38 UTC (permalink / raw)
J�r�mie Lumbroso wrote:
>
> 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, but rather than write it yourself, you could simply instantiate
PragmARC.Wrapping.
http://pragmada.home.mchsi.com/
--
Jeff Carter
"We burst our pimples at you."
Monty Python & the Holy Grail
16
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: Extended 'Succ attribute?
2006-11-23 2:04 ` Adam Beneschan
@ 2006-11-24 13:02 ` Peter Hermann
0 siblings, 0 replies; 4+ messages in thread
From: Peter Hermann @ 2006-11-24 13:02 UTC (permalink / raw)
Adam Beneschan <adam@irvine.com> wrote:
> J?r?mie Lumbroso wrote:
[snip all]
I agree 100% to Adam Benescha.
Moreover, my experience says, that array accesses are fastest.
However, that still depends on tests.
I played around with the following:
-- package Ada.Calendar.Formatting is
-- -- Day of the week:
-- type Day_Name is (Monday, Tuesday, Wednesday, Thursday,
-- Friday, Saturday, Sunday);
-- function Day_of_Week (Date : Time) return Day_Name;
with Ada.Calendar.Formatting;
use Ada.Calendar.Formatting;
with ada.text_io;
procedure ada2005calendartest3ph is
type next_day_type is array(day_name) of day_name;
next_day : next_day_type :=
(Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday, Monday);
specimen : day_name := Friday;
begin
ada.text_io.put_line("starting program ada2005calendartest3ph");
for i in 1..9 loop
ada.text_io.put_line(day_name'image(specimen));
specimen:=next_day(specimen);
end loop;
--result:
--starting program ada2005calendartest3ph
--FRIDAY
--SATURDAY
--SUNDAY
--MONDAY
--TUESDAY
--WEDNESDAY
--THURSDAY
--FRIDAY
--SATURDAY
end ada2005calendartest3ph; --20061124ph
--
--Peter.Hermann@ihr.uni-stuttgart.de (+49)0711-685-872-44(Fax79)
--Nobelstr.19 Raum 0.030, D-70569 Stuttgart IHR Hoechstleistungsrechnen
--http://www.ihr.uni-stuttgart.de/
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2006-11-24 13:02 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-11-23 0:55 Extended 'Succ attribute? Jérémie Lumbroso
2006-11-23 2:04 ` Adam Beneschan
2006-11-24 13:02 ` Peter Hermann
2006-11-23 6:38 ` Jeffrey R. Carter
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox