comp.lang.ada
 help / color / mirror / Atom feed
* Good Ada Style For Impossible Actions?
@ 2002-10-26 15:59 Dr Nancy's Sweetie
  2002-10-26 21:11 ` Jeffrey Carter
  0 siblings, 1 reply; 7+ messages in thread
From: Dr Nancy's Sweetie @ 2002-10-26 15:59 UTC (permalink / raw)



I'm working on a program which uses the ncurses packages to draw on the
screen and handle mouse events.  (It has keyboard input in the event the
user hasn't got a mouse or is using a terminal emulator which doesn't
support a mouse.)  It breaks the working area into four subwindows, only
one of which responds to mouse clicks.

In ncurses, the way to test whether a mouse click happened in a window is
to use the function "Enclosed_In_Window", which takes a window and the
Mouse_Event and returns a Boolean.  But I've moved all the window-handling
code into a separate package.  As a result, the event loop can't see the
window (which is private to the Display object), so the Enclosed_In_Window
line won't compile.

Also, the Mouse_Event has a Row & Column which refer to its actual location
on the screen, not to the logical position within the relevant window.  To
take care of this, I plan to write "Display.Translate_Mouse_Event"
function, which would return the logical position given the Mouse_Event.
But then we come to the situation of "What if the mouse is NOT in the
window?"

At this point, several alternative approaches occurred to me.  The first is
to have the function return an illegal logical location -- made difficult
because the "Location_Type" is range limited to only legal possibilities.
It could be kludged to have a "Any_Location_Type", which would include some
impossible spots, and "Real_Location_Type", which would be only legal
spots, and then cast things around, and then . . .  Well, it's starting to
sound pretty kludgy.  I recognised this from C, in which you return bogus
values for functions that can't do their jobs.  (scanf() returns an EOF,
for example, if you read past end of file.)

The Perl solution might be to use the special value "undef", which is
tested for by the defined() function -- but it seems unlikely that a
strongly-typed language like Ada is going to have an untyped undef.

Considering the parallel with reading input, I could write two functions,
a "Display.Within_Active_Window", returning a Boolean, and then put the
call to Display.Translate_Mouse_Event inside an "if" (following the example
of Ada's "End_Of_File" function, which you call before calling "Get",
instead of calling Get and seeing if it returns a special value).

Then I realised that this could also be done with an exception: I could
make up an exception, "Not_In_Active_Window", have Translate_Mouse_Event
raise that exception if the Mouse_Event wasn't in the window, and then
just have a little begin..exception..end block in the code.

But of these last two approaches, I don't have a feeling for which one is
Good Ada Style(tm).  Should exceptions only be used for serious problems?
Using it here would remove a function and chop out some lines of code, but
it's not really much of a savings and the exception block feels like it
would clutter up the event loop at least as much as the if-then.

Is there some community feeling about how to handle a case like this?  When
to use exceptions and when not to?


My own inclination is to write two functions and use an if, but I thought
it might be worthwhile to ask people with a better sense of the style.


Thank you for your kind attention.


Darren Provine ! kilroy@elvis.rowan.edu ! http://www.rowan.edu/~kilroy
"He seems to me a very foolish man, and very wretched, who will not increase
 his understanding while he is in the world, and ever wish and long to reach
 that endless life where all shall be made clear."
                    -- Alfred the Great, King of the West Saxons (849-899)



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

end of thread, other threads:[~2002-10-29  1:35 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-10-26 15:59 Good Ada Style For Impossible Actions? Dr Nancy's Sweetie
2002-10-26 21:11 ` Jeffrey Carter
2002-10-28  3:52   ` Dr Nancy's Sweetie
2002-10-28 17:13     ` Jeffrey Carter
2002-10-28 17:33       ` tmoran
2002-10-29  1:35         ` Dr Nancy's Sweetie
2002-10-28 17:52       ` Warren W. Gay VE3WWG

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