[Newbies] Enumeration type implementation

Edwin Castro egcastr at gmail.com
Wed Jan 4 18:50:20 UTC 2012


In addition, you might consider using symbols for the name rather than
strings.

On Wed, Jan 4, 2012 at 3:33 AM, Ralph Johnson <johnson at cs.uiuc.edu> wrote:

> That is not bad.   However, EpcimCurrency needs more behavior on the
> instance side.   In particular, a currency should know both its name
> and its description.   i think "value" is just the name.   The
> dictionary should map names to currencies instead of names to strings
> (or descriptions), and the instance should have instance variables
> "name" and "description" instead of "value".   If you do it this way
> then there will only be one instance of each currency and checking for
> equality will be very easy.  But the overall design is on target.
>
> On Wed, Jan 4, 2012 at 5:21 AM, Ben Coman <btc at openinworld.com> wrote:
> > (Resending since it didn't show up on the list overnight)
> >
> > What is the best way to implement "enumerations" in Smalltalk. For
> instance,
> > for a UML definition of...
> > <<enumeration>> Currency
> >  <<enum>> USD   'US dollar'
> >  <<enum>> EUR   'Eueropean euro'
> >  <<enum>> AUD    'Australian dollar'
> >
> > here is my guess, consisting of 2 instance side methods and 3 class side
> > methods...
> > -------------------------
> > Object subclass: #EpcimCurrency
> >  instanceVariableNames: 'value'
> >  classVariableNames: ''
> >  poolDictionaries: ''
> >  category: 'IEC61970-Domain-Enumerations'
> >
> > EpcimCurrency >> value: aCurrency
> >  ( (self class) validate: aCurrency) ifTrue:
> >      [ value := aCurrency
> >      ].
> >
> > EpcimCurrency >> value
> >  ^value
> > -----------------------------------
> >
> > EpcimCurrency class
> >  instanceVariableNames: 'enums'
> >
> > EpcimCurrency class >> validate: aString
> >  enums ifNil: [ self initialize ].
> >  ^ enums includesKey: aString.
> >
> > EpcimCurrency class >>  enums   "for displaying in pulldown menus"
> >  enums ifNil: [ self initialize ].
> >  ^enums copy
> >
> > EpcimCurrency class >> initalize
> >  (enums := Dictionary new)
> >              add: 'USD'-> 'US dollar' ;
> >              add: 'EUR'-> 'European euro' ;
> >              add: 'AUD'-> 'Australian dollar' ;
> >              add: 'CAD'-> 'Canadian dollar' ;
> >              add: 'CHF'-> 'Swiss francs' ;
> >              add: 'CNY'-> 'Chinese yuan renminbi' ;
> >              add: 'DKK'-> 'Danish crown' ;
> >              add: 'GBP'-> 'British pound' ;
> >              add: 'JPY'-> 'Japanese yen' ;
> >              add: 'NOK'-> 'Norwegian crown' ;
> >              add: 'RUR'-> 'Russian ruble' ;
> >              add: 'SEK'-> 'Swedish crown' ;
> >              add: 'INR'-> 'India rupees' ;
> >              add: 'other'-> 'Another type of currency' .
> > ----------------------
> > Examples
> >  " (EpcimCurrency new value: 'AUD' ) value inspect  "      ----> 'AUD'
> >  " (EpcimCurrency new value: 'XXX') value  inspect  "      ----> nil
> >  " EpcimCurrency initialize  "
> >  " EpcimCurrency enums inspect "   ---> aDictionary
> > ------
> > The other way I thought might be like `Color blue`, but I'm not sure
> what is
> > gained.
> >
> > Your feedback would be appreciated.
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > Beginners at lists.squeakfoundation.org
> > http://lists.squeakfoundation.org/mailman/listinfo/beginners
> _______________________________________________
> Beginners mailing list
> Beginners at lists.squeakfoundation.org
> http://lists.squeakfoundation.org/mailman/listinfo/beginners
>



-- 
Edwin G. Castro
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.squeakfoundation.org/pipermail/beginners/attachments/20120104/d651b626/attachment-0001.htm


More information about the Beginners mailing list