2.4 Predicate Objects
The following example exploits this semantics to implement a graphical window object that can be either expanded or iconified. Each of the two important states of the window remembers its own screen location (using a field named position in both cases), plus some other mode-specific information such as the text in the window and the bitmap of the icon, and this data persists across openings and closings of the window:
A window object has twoobjectwindowisainteractive_graphical_object;var fieldiconified(@window) := false;methoddisplay(w@window) { -- draw window usingw.position... }methoderase(w@window) { -- clear space where window is ... }methodmove(w@window, new_position) { -- works for both expanded and iconified windows! w.erase; w.position := new_position; w.display; }predicateexpanded_windowisawindowwhennot(window.iconified);var fieldposition(@expanded_window) := upper_left;fieldtext(@expanded_window);methodiconify(w@expanded_window) { w.erase; w.iconified := true; w.display; }predicateiconified_windowisawindowwhenwindow.iconified;var fieldposition(@iconfied_window) := lower_right;fieldicon(@iconified_window);methodopen(w@iconified_window) { w.erase; w.iconified := false; w.display; }methodcreate_window(open_position, iconified_position, text, icon) {objectisawindow { iconified := false, position@open_window := open_position, position@iconified_window := iconified_position, text := text, icon := icon } }
position fields, but only one is visible at a time. This allows the display, erase, and move routines to send the message position as part of their implementation, without needing to know whether the window is open or closed. The create_window method initializes both position fields when the window is created, even though the position of the icon is not visible initially. The position@object notation used in the field initialization resolves the ambiguity between the two position fields.
Generated with Harlequin WebMaker