Switch.st
author Claus Gittinger <cg@exept.de>
Mon, 27 May 2019 15:02:19 +0200
changeset 4423 bd0b9dbc30c2
parent 3248 ab7cf358e01a
child 3841 a22f33410bdf
permissions -rw-r--r--
#DOCUMENTATION by cg category of: #postCopy #postDeepCopy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34
7ca77877febd Initial revision
claus
parents:
diff changeset
     1
"
7ca77877febd Initial revision
claus
parents:
diff changeset
     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
7ca77877febd Initial revision
claus
parents:
diff changeset
     4
7ca77877febd Initial revision
claus
parents:
diff changeset
     5
 This software is furnished under a license and may be used
7ca77877febd Initial revision
claus
parents:
diff changeset
     6
 only in accordance with the terms of that license and with the
7ca77877febd Initial revision
claus
parents:
diff changeset
     7
 inclusion of the above copyright notice.   This software may not
7ca77877febd Initial revision
claus
parents:
diff changeset
     8
 be provided or otherwise made available to, or used by, any
7ca77877febd Initial revision
claus
parents:
diff changeset
     9
 other person.  No title to or ownership of the software is
7ca77877febd Initial revision
claus
parents:
diff changeset
    10
 hereby transferred.
7ca77877febd Initial revision
claus
parents:
diff changeset
    11
"
2871
d9d2c7abd95c changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 440
diff changeset
    12
"{ Package: 'stx:libcomp' }"
34
7ca77877febd Initial revision
claus
parents:
diff changeset
    13
7ca77877febd Initial revision
claus
parents:
diff changeset
    14
Object subclass:#Switch
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    15
	instanceVariableNames:'expressions values default'
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    16
	classVariableNames:''
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    17
	poolDictionaries:''
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    18
	category:'Programming-Support'
34
7ca77877febd Initial revision
claus
parents:
diff changeset
    19
!
7ca77877febd Initial revision
claus
parents:
diff changeset
    20
7ca77877febd Initial revision
claus
parents:
diff changeset
    21
!Switch class methodsFor:'documentation'!
7ca77877febd Initial revision
claus
parents:
diff changeset
    22
7ca77877febd Initial revision
claus
parents:
diff changeset
    23
copyright
7ca77877febd Initial revision
claus
parents:
diff changeset
    24
"
7ca77877febd Initial revision
claus
parents:
diff changeset
    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
7ca77877febd Initial revision
claus
parents:
diff changeset
    27
7ca77877febd Initial revision
claus
parents:
diff changeset
    28
 This software is furnished under a license and may be used
7ca77877febd Initial revision
claus
parents:
diff changeset
    29
 only in accordance with the terms of that license and with the
7ca77877febd Initial revision
claus
parents:
diff changeset
    30
 inclusion of the above copyright notice.   This software may not
7ca77877febd Initial revision
claus
parents:
diff changeset
    31
 be provided or otherwise made available to, or used by, any
7ca77877febd Initial revision
claus
parents:
diff changeset
    32
 other person.  No title to or ownership of the software is
7ca77877febd Initial revision
claus
parents:
diff changeset
    33
 hereby transferred.
7ca77877febd Initial revision
claus
parents:
diff changeset
    34
"
7ca77877febd Initial revision
claus
parents:
diff changeset
    35
!
7ca77877febd Initial revision
claus
parents:
diff changeset
    36
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    37
documentation
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    38
    "
3248
ab7cf358e01a class: Switch
Stefan Vogel <sv@exept.de>
parents: 2871
diff changeset
    39
     OBSOLETE: do not use it, it is slow. You can also consider the Squeak extension Object>>#caseOf:
ab7cf358e01a class: Switch
Stefan Vogel <sv@exept.de>
parents: 2871
diff changeset
    40
     from the libcompat package.
2871
d9d2c7abd95c changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 440
diff changeset
    41
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    42
     This class provides a switch-expression facility.
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    43
     Consider this a demo example, nested if's are much more
3248
ab7cf358e01a class: Switch
Stefan Vogel <sv@exept.de>
parents: 2871
diff changeset
    44
     efficient.
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    45
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    46
     example:
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    47
440
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    48
        |a b|
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    49
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    50
        a := 1.
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    51
        b := 2.
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    52
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    53
        (Switch new)
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    54
            if:[a > b] then:['a is greater than b'];
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    55
            if:[a < b] then:['a is less than b'];
440
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    56
            otherwise:['a same as b'];
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    57
            value
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    58
    "
34
7ca77877febd Initial revision
claus
parents:
diff changeset
    59
! !
7ca77877febd Initial revision
claus
parents:
diff changeset
    60
7ca77877febd Initial revision
claus
parents:
diff changeset
    61
!Switch class methodsFor:'instance creation'!
7ca77877febd Initial revision
claus
parents:
diff changeset
    62
7ca77877febd Initial revision
claus
parents:
diff changeset
    63
new
7ca77877febd Initial revision
claus
parents:
diff changeset
    64
    ^ self basicNew initialize
7ca77877febd Initial revision
claus
parents:
diff changeset
    65
! !
7ca77877febd Initial revision
claus
parents:
diff changeset
    66
7ca77877febd Initial revision
claus
parents:
diff changeset
    67
!Switch methodsFor:'cases'!
7ca77877febd Initial revision
claus
parents:
diff changeset
    68
7ca77877febd Initial revision
claus
parents:
diff changeset
    69
if:expr then:block
7ca77877febd Initial revision
claus
parents:
diff changeset
    70
    expressions add:expr.
7ca77877febd Initial revision
claus
parents:
diff changeset
    71
    values add:block
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    72
!
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    73
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    74
otherwise:block
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    75
    default := block
34
7ca77877febd Initial revision
claus
parents:
diff changeset
    76
! !
7ca77877febd Initial revision
claus
parents:
diff changeset
    77
7ca77877febd Initial revision
claus
parents:
diff changeset
    78
!Switch methodsFor:'evaluation'!
7ca77877febd Initial revision
claus
parents:
diff changeset
    79
7ca77877febd Initial revision
claus
parents:
diff changeset
    80
value
7ca77877febd Initial revision
claus
parents:
diff changeset
    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
7ca77877febd Initial revision
claus
parents:
diff changeset
    85
    ].
7ca77877febd Initial revision
claus
parents:
diff changeset
    86
    ^ default value
7ca77877febd Initial revision
claus
parents:
diff changeset
    87
! !
7ca77877febd Initial revision
claus
parents:
diff changeset
    88
7ca77877febd Initial revision
claus
parents:
diff changeset
    89
!Switch methodsFor:'initialization'!
7ca77877febd Initial revision
claus
parents:
diff changeset
    90
7ca77877febd Initial revision
claus
parents:
diff changeset
    91
initialize
7ca77877febd Initial revision
claus
parents:
diff changeset
    92
    expressions := OrderedCollection new.
7ca77877febd Initial revision
claus
parents:
diff changeset
    93
    values := OrderedCollection new.
7ca77877febd Initial revision
claus
parents:
diff changeset
    94
! !
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    95
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    96
!Switch class methodsFor:'documentation'!
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    97
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    98
version
3248
ab7cf358e01a class: Switch
Stefan Vogel <sv@exept.de>
parents: 2871
diff changeset
    99
    ^ '$Header: /cvs/stx/stx/libcomp/Switch.st,v 1.8 2013-08-06 09:50:31 stefan Exp $'
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   100
! !
3248
ab7cf358e01a class: Switch
Stefan Vogel <sv@exept.de>
parents: 2871
diff changeset
   101