Is there an equivalent of Java's enumerated values (enum) in Smalltalk?
--- Mark Volkmann
Would this work for you? enum := #(#one #two #three). enum indexOf: #two.
I haven't ever programmed in Java, but I remember when VB had something similar...
I haven't found the need for it in Smalltalk yet, though...however, I'm just getting the hang of it...
Rob
On Fri, Sep 26, 2008 at 6:06 PM, Mark Volkmann mark@ociweb.com wrote:
Is there an equivalent of Java's enumerated values (enum) in Smalltalk?
Mark Volkmann
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
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
I know I didn't ask for this, but thanks for the explanation! I am just starting to think more in objects, although I still find myself trying to push them around. My new mantra is "ask the object to do it for me...ask the object to do it form me..." Fortunately, I am dealing with slightly more concrete objects in Healthcare (patients, bills, supplies, etc...).
Take care,
Rob
On Fri, Sep 26, 2008 at 8:32 PM, Norbert Hartl norbert@hartl.name wrote:
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
Beginners mailing list Beginners@lists.squeakfoundation.org http://lists.squeakfoundation.org/mailman/listinfo/beginners
"Mark" == Mark Volkmann mark@ociweb.com writes:
Mark> Is there an equivalent of Java's enumerated values (enum) in Smalltalk?
I hope not!
beginners@lists.squeakfoundation.org