author | Claus Gittinger <cg@exept.de> |
Sat, 10 Feb 1996 19:33:23 +0100 | |
changeset 212 | ff39051e219f |
parent 135 | aa4f7b8f121e |
child 268 | d768268ace2d |
permissions | -rw-r--r-- |
34 | 1 |
" |
2 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
135
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
3 |
All Rights Reserved |
34 | 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 |
||
13 |
Object subclass:#Switch |
|
14 |
instanceVariableNames:'expressions values default' |
|
15 |
classVariableNames:'' |
|
16 |
poolDictionaries:'' |
|
17 |
category:'System-Misc' |
|
18 |
! |
|
19 |
||
20 |
!Switch class methodsFor:'documentation'! |
|
21 |
||
22 |
documentation |
|
23 |
" |
|
24 |
This class provides a switch-expression facility. |
|
25 |
Consider this a demo example, nested if's are much more |
|
26 |
efficient. However, support for inline switches may be added |
|
27 |
to the compiler (as is done in Smalltalk-agents) |
|
28 |
- in this case, a switch will be as efficient as any other if. |
|
29 |
Testers: Let me know if such a facility is useful and/or needed ... |
|
30 |
||
31 |
example: |
|
32 |
||
135
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
33 |
(Switch new) |
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
34 |
if:[a > b] then:['a is greater than b']; |
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
35 |
if:[a < b] then:['a is less than b']; |
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
36 |
otherwise:['a same as b'; |
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
37 |
value |
34 | 38 |
" |
39 |
! |
|
40 |
||
41 |
copyright |
|
42 |
" |
|
43 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
135
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
44 |
All Rights Reserved |
34 | 45 |
|
46 |
This software is furnished under a license and may be used |
|
47 |
only in accordance with the terms of that license and with the |
|
48 |
inclusion of the above copyright notice. This software may not |
|
49 |
be provided or otherwise made available to, or used by, any |
|
50 |
other person. No title to or ownership of the software is |
|
51 |
hereby transferred. |
|
52 |
" |
|
53 |
! |
|
54 |
||
55 |
version |
|
135
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
56 |
^ '$Header: /cvs/stx/stx/libcomp/Switch.st,v 1.4 1995-11-11 15:31:41 cg Exp $' |
34 | 57 |
! ! |
58 |
||
59 |
!Switch class methodsFor:'instance creation'! |
|
60 |
||
61 |
new |
|
62 |
^ self basicNew initialize |
|
63 |
! ! |
|
64 |
||
65 |
!Switch methodsFor:'cases'! |
|
66 |
||
67 |
otherwise:block |
|
68 |
default := block |
|
69 |
! |
|
70 |
||
71 |
if:expr then:block |
|
72 |
expressions add:expr. |
|
73 |
values add:block |
|
74 |
! ! |
|
75 |
||
76 |
!Switch methodsFor:'evaluation'! |
|
77 |
||
78 |
value |
|
79 |
1 to:expressions size do:[:index | |
|
135
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
80 |
(expressions at:index) value ifTrue:[ |
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
81 |
^ (values at:index) value |
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
82 |
] |
34 | 83 |
]. |
84 |
^ default value |
|
85 |
! ! |
|
86 |
||
87 |
!Switch methodsFor:'initialization'! |
|
88 |
||
89 |
initialize |
|
90 |
expressions := OrderedCollection new. |
|
91 |
values := OrderedCollection new. |
|
92 |
! ! |