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