Switch.st
author Jan Vrany <jan.vrany@labware.com>
Thu, 27 Oct 2022 14:53:59 +0100
branchjv
changeset 4735 3b11fb3ede98
parent 4275 e06f4b087e47
permissions -rw-r--r--
Allow single underscore as method / block argument and temporaries This commit is a follow up for 38b221e.
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
4275
e06f4b087e47 Copyright updates
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3841
diff changeset
     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
7ca77877febd Initial revision
claus
parents:
diff changeset
     5
7ca77877febd Initial revision
claus
parents:
diff changeset
     6
 This software is furnished under a license and may be used
7ca77877febd Initial revision
claus
parents:
diff changeset
     7
 only in accordance with the terms of that license and with the
7ca77877febd Initial revision
claus
parents:
diff changeset
     8
 inclusion of the above copyright notice.   This software may not
7ca77877febd Initial revision
claus
parents:
diff changeset
     9
 be provided or otherwise made available to, or used by, any
7ca77877febd Initial revision
claus
parents:
diff changeset
    10
 other person.  No title to or ownership of the software is
7ca77877febd Initial revision
claus
parents:
diff changeset
    11
 hereby transferred.
7ca77877febd Initial revision
claus
parents:
diff changeset
    12
"
2871
d9d2c7abd95c changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 440
diff changeset
    13
"{ Package: 'stx:libcomp' }"
34
7ca77877febd Initial revision
claus
parents:
diff changeset
    14
3841
a22f33410bdf Reduced dependencies
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3248
diff changeset
    15
"{ NameSpace: Smalltalk }"
a22f33410bdf Reduced dependencies
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3248
diff changeset
    16
34
7ca77877febd Initial revision
claus
parents:
diff changeset
    17
Object subclass:#Switch
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    18
	instanceVariableNames:'expressions values default'
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    19
	classVariableNames:''
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    20
	poolDictionaries:''
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    21
	category:'Programming-Support'
34
7ca77877febd Initial revision
claus
parents:
diff changeset
    22
!
7ca77877febd Initial revision
claus
parents:
diff changeset
    23
7ca77877febd Initial revision
claus
parents:
diff changeset
    24
!Switch class methodsFor:'documentation'!
7ca77877febd Initial revision
claus
parents:
diff changeset
    25
7ca77877febd Initial revision
claus
parents:
diff changeset
    26
copyright
7ca77877febd Initial revision
claus
parents:
diff changeset
    27
"
7ca77877febd Initial revision
claus
parents:
diff changeset
    28
 COPYRIGHT (c) 1994 by Claus Gittinger
4275
e06f4b087e47 Copyright updates
Jan Vrany <jan.vrany@fit.cvut.cz>
parents: 3841
diff changeset
    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
7ca77877febd Initial revision
claus
parents:
diff changeset
    31
7ca77877febd Initial revision
claus
parents:
diff changeset
    32
 This software is furnished under a license and may be used
7ca77877febd Initial revision
claus
parents:
diff changeset
    33
 only in accordance with the terms of that license and with the
7ca77877febd Initial revision
claus
parents:
diff changeset
    34
 inclusion of the above copyright notice.   This software may not
7ca77877febd Initial revision
claus
parents:
diff changeset
    35
 be provided or otherwise made available to, or used by, any
7ca77877febd Initial revision
claus
parents:
diff changeset
    36
 other person.  No title to or ownership of the software is
7ca77877febd Initial revision
claus
parents:
diff changeset
    37
 hereby transferred.
7ca77877febd Initial revision
claus
parents:
diff changeset
    38
"
7ca77877febd Initial revision
claus
parents:
diff changeset
    39
!
7ca77877febd Initial revision
claus
parents:
diff changeset
    40
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    41
documentation
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    42
    "
3248
ab7cf358e01a class: Switch
Stefan Vogel <sv@exept.de>
parents: 2871
diff changeset
    43
     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
    44
     from the libcompat package.
2871
d9d2c7abd95c changed: #documentation
Claus Gittinger <cg@exept.de>
parents: 440
diff changeset
    45
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    46
     This class provides a switch-expression facility.
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    47
     Consider this a demo example, nested if's are much more
3248
ab7cf358e01a class: Switch
Stefan Vogel <sv@exept.de>
parents: 2871
diff changeset
    48
     efficient.
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    49
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    50
     example:
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    51
440
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    52
        |a b|
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    53
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    54
        a := 1.
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    55
        b := 2.
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    56
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    57
        (Switch new)
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    58
            if:[a > b] then:['a is greater than b'];
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    59
            if:[a < b] then:['a is less than b'];
440
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    60
            otherwise:['a same as b'];
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    61
            value
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    62
    "
34
7ca77877febd Initial revision
claus
parents:
diff changeset
    63
! !
7ca77877febd Initial revision
claus
parents:
diff changeset
    64
7ca77877febd Initial revision
claus
parents:
diff changeset
    65
!Switch class methodsFor:'instance creation'!
7ca77877febd Initial revision
claus
parents:
diff changeset
    66
7ca77877febd Initial revision
claus
parents:
diff changeset
    67
new
7ca77877febd Initial revision
claus
parents:
diff changeset
    68
    ^ self basicNew initialize
7ca77877febd Initial revision
claus
parents:
diff changeset
    69
! !
7ca77877febd Initial revision
claus
parents:
diff changeset
    70
7ca77877febd Initial revision
claus
parents:
diff changeset
    71
!Switch methodsFor:'cases'!
7ca77877febd Initial revision
claus
parents:
diff changeset
    72
7ca77877febd Initial revision
claus
parents:
diff changeset
    73
if:expr then:block
7ca77877febd Initial revision
claus
parents:
diff changeset
    74
    expressions add:expr.
7ca77877febd Initial revision
claus
parents:
diff changeset
    75
    values add:block
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    76
!
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    77
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    78
otherwise:block
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    79
    default := block
34
7ca77877febd Initial revision
claus
parents:
diff changeset
    80
! !
7ca77877febd Initial revision
claus
parents:
diff changeset
    81
7ca77877febd Initial revision
claus
parents:
diff changeset
    82
!Switch methodsFor:'evaluation'!
7ca77877febd Initial revision
claus
parents:
diff changeset
    83
7ca77877febd Initial revision
claus
parents:
diff changeset
    84
value
7ca77877febd Initial revision
claus
parents:
diff changeset
    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
7ca77877febd Initial revision
claus
parents:
diff changeset
    89
    ].
7ca77877febd Initial revision
claus
parents:
diff changeset
    90
    ^ default value
7ca77877febd Initial revision
claus
parents:
diff changeset
    91
! !
7ca77877febd Initial revision
claus
parents:
diff changeset
    92
7ca77877febd Initial revision
claus
parents:
diff changeset
    93
!Switch methodsFor:'initialization'!
7ca77877febd Initial revision
claus
parents:
diff changeset
    94
7ca77877febd Initial revision
claus
parents:
diff changeset
    95
initialize
7ca77877febd Initial revision
claus
parents:
diff changeset
    96
    expressions := OrderedCollection new.
7ca77877febd Initial revision
claus
parents:
diff changeset
    97
    values := OrderedCollection new.
7ca77877febd Initial revision
claus
parents:
diff changeset
    98
! !
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    99
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   100
!Switch class methodsFor:'documentation'!
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   101
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   102
version
3248
ab7cf358e01a class: Switch
Stefan Vogel <sv@exept.de>
parents: 2871
diff changeset
   103
    ^ '$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
   104
! !
3248
ab7cf358e01a class: Switch
Stefan Vogel <sv@exept.de>
parents: 2871
diff changeset
   105