Switch.st
author Claus Gittinger <cg@exept.de>
Tue, 26 Dec 1995 18:29:19 +0100
changeset 179 56b3bd870f07
parent 135 aa4f7b8f121e
child 268 d768268ace2d
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1994 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

Object subclass:#Switch
	 instanceVariableNames:'expressions values default'
	 classVariableNames:''
	 poolDictionaries:''
	 category:'System-Misc'
!

!Switch class methodsFor:'documentation'!

documentation
    "
     This class provides a switch-expression facility.
     Consider this a demo example, nested if's are much more
     efficient. However, support for inline switches may be added
     to the compiler (as is done in Smalltalk-agents)
     - in this case, a switch will be as efficient as any other if.
     Testers: Let me know if such a facility is useful and/or needed ...

     example:

	(Switch new)
	    if:[a > b] then:['a is greater than b'];
	    if:[a < b] then:['a is less than b'];
	    otherwise:['a same as b';
	    value
    "
!

copyright
"
 COPYRIGHT (c) 1994 by Claus Gittinger
	      All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

version
    ^ '$Header: /cvs/stx/stx/libcomp/Switch.st,v 1.4 1995-11-11 15:31:41 cg Exp $'
! !

!Switch class methodsFor:'instance creation'!

new
    ^ self basicNew initialize
! !

!Switch methodsFor:'cases'!

otherwise:block
    default := block
!

if:expr then:block
    expressions add:expr.
    values add:block
! !

!Switch methodsFor:'evaluation'!

value
    1 to:expressions size do:[:index |
	(expressions at:index) value ifTrue:[
	    ^ (values at:index) value
	]
    ].
    ^ default value
! !

!Switch methodsFor:'initialization'!

initialize
    expressions := OrderedCollection new.
    values := OrderedCollection new.
! !