author | Jan Vrany <jan.vrany@labware.com> |
Wed, 03 Feb 2021 11:52:30 +0000 | |
branch | jv |
changeset 25410 | c91ea94445d1 |
parent 19227 | 5e949760a4e8 |
permissions | -rw-r--r-- |
6432 | 1 |
" |
2 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
3 |
All Rights Reserved |
|
4 |
||
5 |
This software is furnished under a license and may be used |
|
6 |
only in accordance with the terms of that license and with the |
|
7 |
inclusion of the above copyright notice. This software may not |
|
8 |
be provided or otherwise made available to, or used by, any |
|
9 |
other person. No title to or ownership of the software is |
|
10 |
hereby transferred. |
|
11 |
" |
|
12 |
"{ Package: 'stx:libbasic' }" |
|
13 |
||
19202
f310f0e159f9
comment/format in: #iconInBrowserSymbol
Claus Gittinger <cg@exept.de>
parents:
12088
diff
changeset
|
14 |
"{ NameSpace: Smalltalk }" |
f310f0e159f9
comment/format in: #iconInBrowserSymbol
Claus Gittinger <cg@exept.de>
parents:
12088
diff
changeset
|
15 |
|
6432 | 16 |
Notification subclass:#Query |
17 |
instanceVariableNames:'' |
|
18 |
classVariableNames:'' |
|
19 |
poolDictionaries:'' |
|
20 |
category:'Kernel-Exceptions' |
|
21 |
! |
|
22 |
||
23 |
!Query class methodsFor:'documentation'! |
|
24 |
||
25 |
copyright |
|
26 |
" |
|
27 |
COPYRIGHT (c) 2002 by eXept Software AG |
|
28 |
All Rights Reserved |
|
29 |
||
30 |
This software is furnished under a license and may be used |
|
31 |
only in accordance with the terms of that license and with the |
|
32 |
inclusion of the above copyright notice. This software may not |
|
33 |
be provided or otherwise made available to, or used by, any |
|
34 |
other person. No title to or ownership of the software is |
|
35 |
hereby transferred. |
|
36 |
" |
|
37 |
! |
|
38 |
||
39 |
documentation |
|
40 |
" |
|
41 |
Query is an abstract superclass for queries. |
|
42 |
||
43 |
A query is an exception which by default proceeds if unhandled and returns |
|
44 |
a default value. |
|
45 |
As a class based reimplementation, it replaces and obsoletes the old |
|
46 |
QuerySignal instance based mechanism. |
|
47 |
||
48 |
Note: |
|
49 |
Query does not add/refine any functionality from its superclass. |
|
50 |
It exists for the more descriptive class name only. |
|
51 |
||
11201 | 52 |
Queries are like exceptions, except that they are not accepted |
53 |
by handlers for ordinary exceptions. |
|
54 |
I.e. a handler for a normal exception will not handle a query. |
|
55 |
Thus, these bypass all normal exception handlers. |
|
56 |
||
57 |
However, if unhandled, no error is raised, instead it is simply ignored |
|
58 |
and nil is returned from the raise |
|
59 |
(as opposed to normal exceptions, which raise an unhandled exception error). |
|
60 |
Queries are also ignored, if a handler exists, but rejects. |
|
61 |
||
62 |
The main use of Queries is to implement an upQuery, which works even |
|
63 |
if intermediate errorSignal handlers are present. |
|
64 |
||
65 |
Code deep down in the calling hierarchy can post such an up-Query to ask |
|
66 |
for some information or to pass some information upward. |
|
67 |
||
68 |
For example, the activityNotification mechanism is built on top of this: |
|
69 |
everyone can send such a notification which is either handled by someone |
|
70 |
up in the hierarchy (to show it in the windows info area) or simply |
|
71 |
ignored. |
|
72 |
||
73 |
Using Queries for this (instead of regular Signals) helps in documenting |
|
74 |
the intended usage of those signals. |
|
75 |
||
76 |
Another use of queries is to provide additional information to |
|
77 |
deeply nested methods, which is only required in the uncommon case; |
|
78 |
or if another parameter is required by some method, which was not planned |
|
79 |
for in the beginning, and you do not want to hand this value (via an |
|
80 |
additional argument) through all intermediate levels. |
|
81 |
||
82 |
A highly elegant solution to this problem is to provide a handler somewhere |
|
83 |
at the top of the calling hierarchy, and raise an upQuery from whereever |
|
84 |
that value is required. |
|
85 |
A concrete application can be found in the windowGroup-lastEvent |
|
86 |
queries. If anyone is interested in the windowEvent which was responible for |
|
87 |
being invoked, all he needs to do is to raise the lastEventQuerySignal, |
|
88 |
which returns that event. |
|
89 |
No intermediate methods are required to know anything about that. |
|
90 |
Another example is found in the way Metaclass asks for the nameSpace |
|
91 |
when new classes are to be installed. A Browser may simply answer such |
|
92 |
a query and provide a namespace (no need to pass that information down |
|
93 |
the calling chain). |
|
94 |
||
95 |
A final note (to C++ and Java fans): |
|
96 |
such upQueries are only possible, if the exception handling mechanism |
|
97 |
does not automatically unwind the stack for the handler invokation. |
|
98 |
Since the handler must be able to proceed the execution and return |
|
99 |
a value to the raiser .... |
|
100 |
... another demonstration of why ST's exception mechanisms are superior. |
|
101 |
||
6432 | 102 |
[author:] |
103 |
Claus Gittinger |
|
104 |
||
105 |
[see also:] |
|
11201 | 106 |
Notification Warning Signal QuerySignal |
107 |
(``Exception handling and signals'': programming/exceptions.html) |
|
6432 | 108 |
" |
109 |
! |
|
110 |
||
111 |
examples |
|
112 |
" |
|
113 |
[exBegin] |
|
114 |
Query answer:'hello' |
|
115 |
do:[ |
|
116 |
Transcript showCR:(Query query) |
|
117 |
] |
|
118 |
[exEnd] |
|
119 |
||
120 |
||
121 |
an up-query from a deeply nested operation to a higher level: |
|
122 |
the example below demonstrates that a Query is not an Error |
|
123 |
(i.e. the Error-handler does not interfere with Queries) |
|
124 |
[exBegin] |
|
125 |
|zero| |
|
126 |
||
127 |
zero := 0. |
|
128 |
Query handle:[:ex | |
|
129 |
Transcript showCR:'query'. |
|
130 |
ex proceedWith:true |
|
131 |
] do:[ |
|
132 |
'nesting'. |
|
133 |
[ |
|
134 |
[ |
|
135 |
Error handle:[:ex | |
|
136 |
Transcript showCR:'some error: ' , ex errorString. |
|
137 |
ex proceed |
|
138 |
] do:[ |
|
139 |
[ |
|
140 |
1 // zero. 'an error which is caught in the handler'. |
|
141 |
(Query query) == true ifTrue:[ |
|
142 |
Transcript showCR:'query says: ok'. |
|
143 |
] ifFalse:[ |
|
144 |
Transcript showCR:'query says: no' |
|
145 |
] |
|
146 |
] value |
|
147 |
] |
|
148 |
] value |
|
149 |
] value |
|
150 |
] |
|
151 |
[exEnd] |
|
152 |
for lazy typists, a more compact interface for query-answerign |
|
153 |
is also provided (which is also easier to read): |
|
154 |
[exBegin] |
|
155 |
Query answer:true do:[ |
|
156 |
'nesting'. |
|
157 |
[ |
|
158 |
[ |
|
159 |
(Query query) == true ifTrue:[ |
|
160 |
Transcript showCR:'query says: ok'. |
|
161 |
] ifFalse:[ |
|
162 |
Transcript showCR:'query says: no' |
|
163 |
] |
|
164 |
] value |
|
165 |
] value |
|
166 |
] |
|
167 |
[exEnd] |
|
168 |
an up-query from a deeply nested operation, for which there |
|
169 |
is no handler: |
|
170 |
(notice, this would not work with normal signals, which would raise |
|
171 |
another unhandled exception-exception; |
|
172 |
also notice the == check #raise's return value being true, |
|
173 |
instead of a simple ifTrue; this handles a nil-value from |
|
174 |
the unhandled query) |
|
175 |
[exBegin] |
|
176 |
|zero| |
|
177 |
||
178 |
zero := 0. |
|
179 |
[ |
|
180 |
'nesting'. |
|
181 |
[ |
|
182 |
[ |
|
183 |
Error handle:[:ex | |
|
184 |
Transcript showCR:'some error: ' , ex errorString. |
|
185 |
ex proceed |
|
186 |
] do:[ |
|
187 |
[ |
|
188 |
1 // zero. 'an error which is caught in the handler'. |
|
189 |
(Query raise) == true ifTrue:[ |
|
190 |
Transcript showCR:'query says: ok'. |
|
191 |
] ifFalse:[ |
|
192 |
Transcript showCR:'query says: no' |
|
193 |
] |
|
194 |
] value |
|
195 |
] |
|
196 |
] value |
|
197 |
] value |
|
198 |
] value |
|
199 |
[exEnd] |
|
200 |
counter-example, just to show that things would not work this way |
|
201 |
with regular signals: |
|
202 |
[exBegin] |
|
203 |
|signal| |
|
204 |
||
205 |
signal := Signal new. |
|
206 |
'nesting deeply'. |
|
207 |
[ |
|
208 |
[ |
|
209 |
[ |
|
210 |
[ |
|
211 |
[ |
|
212 |
(signal raise) == true ifTrue:[ |
|
213 |
Transcript showCR:'query says: ok'. |
|
214 |
] ifFalse:[ |
|
215 |
Transcript showCR:'query says: no' |
|
216 |
] |
|
217 |
] value |
|
218 |
] value |
|
219 |
] value |
|
220 |
] value |
|
221 |
] value |
|
222 |
[exEnd] |
|
223 |
||
224 |
except, by handling the unhandled exception |
|
225 |
(but we think, that querySignals are easier to use and |
|
226 |
better document the intent): |
|
227 |
[exBegin] |
|
228 |
|signal| |
|
229 |
||
230 |
signal := Signal new. |
|
231 |
'nesting deeply'. |
|
232 |
[ |
|
233 |
[ |
|
234 |
[ |
|
235 |
[ |
|
236 |
[ |
|
237 |
Signal noHandlerSignal handle:[:ex | |
|
238 |
ex proceedWith:nil |
|
239 |
] do:[ |
|
240 |
(signal raise) == true ifTrue:[ |
|
241 |
Transcript showCR:'query says: ok'. |
|
242 |
] ifFalse:[ |
|
243 |
Transcript showCR:'query says: no' |
|
244 |
] |
|
245 |
] |
|
246 |
] value |
|
247 |
] value |
|
248 |
] value |
|
249 |
] value |
|
250 |
] value |
|
251 |
[exEnd] |
|
252 |
" |
|
253 |
! ! |
|
254 |
||
255 |
!Query class methodsFor:'initialization'! |
|
256 |
||
257 |
initialize |
|
258 |
||
259 |
NotifierString := 'Query'. |
|
260 |
||
261 |
" |
|
262 |
self initialize |
|
263 |
" |
|
264 |
! ! |
|
265 |
||
12088 | 266 |
|
6433 | 267 |
!Query class methodsFor:'documentation'! |
268 |
||
269 |
version |
|
19202
f310f0e159f9
comment/format in: #iconInBrowserSymbol
Claus Gittinger <cg@exept.de>
parents:
12088
diff
changeset
|
270 |
^ '$Header$' |
12088 | 271 |
! |
272 |
||
273 |
version_CVS |
|
19202
f310f0e159f9
comment/format in: #iconInBrowserSymbol
Claus Gittinger <cg@exept.de>
parents:
12088
diff
changeset
|
274 |
^ '$Header$' |
6433 | 275 |
! ! |
8681
38aa963476a5
Support for Block>>#on:do:on:do:
Stefan Vogel <sv@exept.de>
parents:
6433
diff
changeset
|
276 |
|
19202
f310f0e159f9
comment/format in: #iconInBrowserSymbol
Claus Gittinger <cg@exept.de>
parents:
12088
diff
changeset
|
277 |
|
6432 | 278 |
Query initialize! |