author | Claus Gittinger <cg@exept.de> |
Wed, 26 Jun 2019 20:02:58 +0200 | |
changeset 4451 | a6536ecebfb5 |
parent 3248 | ab7cf358e01a |
child 3841 | a22f33410bdf |
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 |
" |
|
2871 | 12 |
"{ Package: 'stx:libcomp' }" |
34 | 13 |
|
14 |
Object subclass:#Switch |
|
268 | 15 |
instanceVariableNames:'expressions values default' |
16 |
classVariableNames:'' |
|
17 |
poolDictionaries:'' |
|
18 |
category:'Programming-Support' |
|
34 | 19 |
! |
20 |
||
21 |
!Switch class methodsFor:'documentation'! |
|
22 |
||
23 |
copyright |
|
24 |
" |
|
25 |
COPYRIGHT (c) 1994 by Claus Gittinger |
|
135
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
26 |
All Rights Reserved |
34 | 27 |
|
28 |
This software is furnished under a license and may be used |
|
29 |
only in accordance with the terms of that license and with the |
|
30 |
inclusion of the above copyright notice. This software may not |
|
31 |
be provided or otherwise made available to, or used by, any |
|
32 |
other person. No title to or ownership of the software is |
|
33 |
hereby transferred. |
|
34 |
" |
|
35 |
! |
|
36 |
||
268 | 37 |
documentation |
38 |
" |
|
3248 | 39 |
OBSOLETE: do not use it, it is slow. You can also consider the Squeak extension Object>>#caseOf: |
40 |
from the libcompat package. |
|
2871 | 41 |
|
268 | 42 |
This class provides a switch-expression facility. |
43 |
Consider this a demo example, nested if's are much more |
|
3248 | 44 |
efficient. |
268 | 45 |
|
46 |
example: |
|
47 |
||
440 | 48 |
|a b| |
49 |
||
50 |
a := 1. |
|
51 |
b := 2. |
|
52 |
||
268 | 53 |
(Switch new) |
54 |
if:[a > b] then:['a is greater than b']; |
|
55 |
if:[a < b] then:['a is less than b']; |
|
440 | 56 |
otherwise:['a same as b']; |
268 | 57 |
value |
58 |
" |
|
34 | 59 |
! ! |
60 |
||
61 |
!Switch class methodsFor:'instance creation'! |
|
62 |
||
63 |
new |
|
64 |
^ self basicNew initialize |
|
65 |
! ! |
|
66 |
||
67 |
!Switch methodsFor:'cases'! |
|
68 |
||
69 |
if:expr then:block |
|
70 |
expressions add:expr. |
|
71 |
values add:block |
|
268 | 72 |
! |
73 |
||
74 |
otherwise:block |
|
75 |
default := block |
|
34 | 76 |
! ! |
77 |
||
78 |
!Switch methodsFor:'evaluation'! |
|
79 |
||
80 |
value |
|
81 |
1 to:expressions size do:[:index | |
|
135
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
82 |
(expressions at:index) value ifTrue:[ |
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
83 |
^ (values at:index) value |
aa4f7b8f121e
uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents:
103
diff
changeset
|
84 |
] |
34 | 85 |
]. |
86 |
^ default value |
|
87 |
! ! |
|
88 |
||
89 |
!Switch methodsFor:'initialization'! |
|
90 |
||
91 |
initialize |
|
92 |
expressions := OrderedCollection new. |
|
93 |
values := OrderedCollection new. |
|
94 |
! ! |
|
268 | 95 |
|
96 |
!Switch class methodsFor:'documentation'! |
|
97 |
||
98 |
version |
|
3248 | 99 |
^ '$Header: /cvs/stx/stx/libcomp/Switch.st,v 1.8 2013-08-06 09:50:31 stefan Exp $' |
268 | 100 |
! ! |
3248 | 101 |