The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Switch Statements

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
James Robertson

Posts: 29924
Nickname: jarober61
Registered: Jun, 2003

David Buck, Smalltalker at large
Switch Statements Posted: Jun 15, 2004 11:44 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: Switch Statements
Feed Title: Cincom Smalltalk Blog - Smalltalk with Rants
Feed URL: http://www.cincomsmalltalk.com/rssBlog/rssBlogView.xml
Feed Description: James Robertson comments on Cincom Smalltalk, the Smalltalk development community, and IT trends and issues in general.
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From Cincom Smalltalk Blog - Smalltalk with Rants

Advertisement

Every so often I see people ask "Why doesn't Smalltalk have a switch statement"? The simple answer is - "we don't need one". That usually ends up with something like this:

What if I'm parsing data that comes in with numeric (etc) values, and I want to take a different action on the different values?

Here's one approach to that problem - say you are going to get integers, and you need to execute a different action on receiving each:


setupActionDictionary
	self actionDictionary: Dictionary new.
	self actionDictionary at: 1 put: #handleOne.
	self actionDictionary at: 2 put: #handleTwo.
	...


And so on like that. So now, we can handle the receipt of the data like this:


executeActionFor: anID
	action := self actionDictionary at: anID ifAbsent: [#defaultAction].
	self perform: action.

actionID := self getNextIdentifier. self executeActionFor: anID.

That's about it. For those of you who don't know Smalltalk, the keys in the dictionary are symbols (which can be mapped to method names). Hopefully, those names would be better than #handleOne (etc), but you get the idea.

Read: Switch Statements

Topic: An all too common thought process Previous Topic   Next Topic Topic: Colorful Extensions

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use