[Newbies] enumerated values

Norbert Hartl norbert at hartl.name
Sat Sep 27 00:32:07 UTC 2008


On Fri, 2008-09-26 at 17:06 -0500, Mark Volkmann wrote:
> Is there an equivalent of Java's enumerated values (enum) in Smalltalk?
> 
Yes and a very good one. We call it class! :))

A usage pattern I see often is this one (in java):

- define an Enum MyEnum with constants: one two three
- anywhere in the code assign MyEnum.two to variable myCase
- later build a case construct that selects a body of
  code to execute depending on the value of myCase
- execute the selected code (that prints something)

So we would do:

- define a class MyEnum and class methods for one, two, three 
  to create instances of MyEnum which are initialized differently.
  MyEnum has a method printSomething which contains the same
  code as in the enum example
- anywhere in the code assign MyEnum two to myCase
- later invoke myCase printSomething 

The difference between those two is that we use the builtin
case construct that is called method lookup. And it is less
code and better to read. And your constant is an object that
is alive and could provide you with many more things than
printSomething. 

Just ask if this isn't clean as I might think.

Norbert





More information about the Beginners mailing list