From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,ea2b3498720dfde1,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-10-26 09:06:16 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!iad-peer.news.verio.net!news.verio.net!iad-read.news.verio.net.POSTED!not-for-mail From: Dr Nancy's Sweetie Subject: Good Ada Style For Impossible Actions? Newsgroups: comp.lang.ada Organization: Rowan College of New Jersey User-Agent: tin/1.4.6-20020816 ("Aerials") (UNIX) (SunOS/5.8 (sun4u)) Message-ID: Date: Sat, 26 Oct 2002 15:59:49 GMT NNTP-Posting-Host: 150.250.64.69 X-Complaints-To: abuse@verio.net X-Trace: iad-read.news.verio.net 1035647989 150.250.64.69 (Sat, 26 Oct 2002 15:59:49 GMT) NNTP-Posting-Date: Sat, 26 Oct 2002 15:59:49 GMT Xref: archiver1.google.com comp.lang.ada:30157 Date: 2002-10-26T15:59:49+00:00 List-Id: 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)