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