The Artima Developer Community
Sponsored Link

Agile Buzz Forum
Private Methods in Smalltalk

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
Private Methods in Smalltalk Posted: Sep 19, 2005 9:21 PM
Reply to this message Reply

This post originated from an RSS feed registered with Agile Buzz by James Robertson.
Original Post: Private Methods in Smalltalk
Feed Title: David Buck - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/buck-rss.xml
Feed Description: Smalltalk can do that
Latest Agile Buzz Posts
Latest Agile Buzz Posts by James Robertson
Latest Posts From David Buck - Blog

Advertisement

Charles Monteiro is concerned about creating accessors that are only needed by class methods to set up the instance. Rather than using a special syntax for the class method, I would rather simply declare the instance method as being private. But every Smalltalker knows that methods aren't really private. Anyone can call any method in the system.

Although this may be theoretically true, in practice it is possible to make a method private. In VisualWorks, you just write the method like this:

property: anObject    
    self privateMethod.  
    property := anObject
 

Now, you just have to write privateMethod. You can do that like this:

privateMethod
	(thisContext sender sender method mclass == self class
 		| (thisContext sender sender method mclass == self class class))
  		ifFalse: [PrivateMethodException raise] 
 

This allows the method to be called by instance methods and class methods of the same class as the receiver.

Do you want protected methods that can only be called by class or instance methods of subclasses of the method? Try this:

protectedMethod
	(self class isKindOf: thisContext sender sender method mclass
 		| (self class class isKindOf: thisContext sender sender method mclass))
  		ifFalse: [PrivateMethodException raise] 
 

Private methods can be sent from one instance to another instance of the same class. How about a method that can only be called from other methods on the same instance? Try this:

selfPrivateMethod
	thisContext sender sender receiver == self
 		ifFalse: [PrivateMethodException raise] 
 

In fact, you could come up with methods that allow calls from a set of classes. You have much more flexibility than you would if private and protected were built into the language syntax.

Read: Private Methods in Smalltalk

Topic: * cough* Ease of Use *cough* and Media Center PC Previous Topic   Next Topic Topic: Recently Spotted...

Sponsored Links



Google
  Web Artima.com   

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