Switch.st
author Stefan Vogel <sv@exept.de>
Fri, 06 Mar 1998 16:38:37 +0100
changeset 657 0ecf1ff6f6bf
parent 440 a9f3ecf2aa6f
child 2871 d9d2c7abd95c
permissions -rw-r--r--
Fix #makeMethod:
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
"
7ca77877febd Initial revision
claus
parents:
diff changeset
    12
7ca77877febd Initial revision
claus
parents:
diff changeset
    13
Object subclass:#Switch
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    14
	instanceVariableNames:'expressions values default'
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    15
	classVariableNames:''
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    16
	poolDictionaries:''
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    17
	category:'Programming-Support'
34
7ca77877febd Initial revision
claus
parents:
diff changeset
    18
!
7ca77877febd Initial revision
claus
parents:
diff changeset
    19
7ca77877febd Initial revision
claus
parents:
diff changeset
    20
!Switch class methodsFor:'documentation'!
7ca77877febd Initial revision
claus
parents:
diff changeset
    21
7ca77877febd Initial revision
claus
parents:
diff changeset
    22
copyright
7ca77877febd Initial revision
claus
parents:
diff changeset
    23
"
7ca77877febd Initial revision
claus
parents:
diff changeset
    24
 COPYRIGHT (c) 1994 by Claus Gittinger
135
aa4f7b8f121e uff - version methods changed to return stings
Claus Gittinger <cg@exept.de>
parents: 103
diff changeset
    25
	      All Rights Reserved
34
7ca77877febd Initial revision
claus
parents:
diff changeset
    26
7ca77877febd Initial revision
claus
parents:
diff changeset
    27
 This software is furnished under a license and may be used
7ca77877febd Initial revision
claus
parents:
diff changeset
    28
 only in accordance with the terms of that license and with the
7ca77877febd Initial revision
claus
parents:
diff changeset
    29
 inclusion of the above copyright notice.   This software may not
7ca77877febd Initial revision
claus
parents:
diff changeset
    30
 be provided or otherwise made available to, or used by, any
7ca77877febd Initial revision
claus
parents:
diff changeset
    31
 other person.  No title to or ownership of the software is
7ca77877febd Initial revision
claus
parents:
diff changeset
    32
 hereby transferred.
7ca77877febd Initial revision
claus
parents:
diff changeset
    33
"
7ca77877febd Initial revision
claus
parents:
diff changeset
    34
!
7ca77877febd Initial revision
claus
parents:
diff changeset
    35
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    36
documentation
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    37
    "
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    38
     This class provides a switch-expression facility.
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    39
     Consider this a demo example, nested if's are much more
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    40
     efficient. However, support for inline switches may be added
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    41
     to the compiler (as is done in Smalltalk-agents)
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    42
     - in this case, a switch will be as efficient as any other if.
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    43
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
    44
     Beta Testers: Let me know if such a facility is useful and/or needed ...
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
440
a9f3ecf2aa6f Fix typo.
Stefan Vogel <sv@exept.de>
parents: 268
diff changeset
    99
    ^ '$Header: /cvs/stx/stx/libcomp/Switch.st,v 1.6 1996-11-11 21:54:08 stefan Exp $'
268
d768268ace2d checkin from browser
Claus Gittinger <cg@exept.de>
parents: 135
diff changeset
   100
! !