enumerated values

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

enumerated values

Mark Volkmann
Is there an equivalent of Java's enumerated values (enum) in Smalltalk?

---
Mark Volkmann




_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: enumerated values

Rob Rothwell
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 <[hidden email]> wrote:
Is there an equivalent of Java's enumerated values (enum) in Smalltalk?

---
Mark Volkmann




_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: enumerated values

NorbertHartl
In reply to this post by Mark Volkmann
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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: enumerated values

Rob Rothwell
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 <[hidden email]> 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
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: enumerated values

Randal L. Schwartz
In reply to this post by Mark Volkmann
>>>>> "Mark" == Mark Volkmann <[hidden email]> writes:

Mark> Is there an equivalent of Java's enumerated values (enum) in Smalltalk?

I hope not!

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<[hidden email]> <URL:http://www.stonehenge.com/merlyn/>
Smalltalk/Perl/Unix consulting, Technical writing, Comedy, etc. etc.
See http://methodsandmessages.vox.com/ for Smalltalk and Seaside discussion
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners