Search code examples
smalltalk

Smalltalk - Dictionary equivalent that takes alphanumeric as key?


Is there a Dictionary equivalent that will accept alphanumerics as the key? After spending some time on here getting you guys to help with assigning UUID values, I now realize that Dictionaries only take integers. I had planned on using the UUID as the key.

My design is for a General Ledger - my ledger member is initialized as ledger := Dictionary new, and I had hoped on doing something along the lines of:

postTransaction: GLEntry
    ledger at: GLEntry UID put: GLEntry

Solution

  • A Dictionary can use anything you like as a key. Using Strings for keys in a Dictionary is quite common. For example, in Dolphin Smalltalk v6.x evaluating the following:

    d := Dictionary new.
    
    d
      at: 'a' put: 'a string';
      at: 'b' put: 'b string';
      at: 'c' put: 'c string'.
    
    Transcript
      show: (d at: 'a'); cr;
      show: (d at: 'b'); cr;
      show: (d at: 'c'); cr
    

    will result in

    a string
    b string
    c string
    

    being shown in the Transcript window.

    Share and enjoy.