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