whitenoisedb
whitenoisedb

Reputation: 403

Class as parameter in Smalltalk

I need to know if it is possible to pass a Class as a parameter to a method in Smalltalk. For example,

Classname>>method: anObject
self anotherMethod: aClass with: anObject.


Classname>>anotherMethod: aClass with: anObject.
|instance|
instance:= aClass new: anObject aMessage. //supposing "new:" is the instance method of aClass.
instance aMethodFromTheClassRecieved.

Upvotes: 0

Views: 203

Answers (2)

Uko
Uko

Reputation: 13386

If you have:

Classname>>anotherMethod: aClass
  ^ aClass new.

and you execute something like:

instance anotherMethod: OrderedCollection

Then you'll get an instance of OrderedCollection.

In Smalltalk classes are objects too, so if you do OrderedCollection new you actually sent #new message to OrderedCollection class object. So you can pass classes around just like the other objects.

P.S. The main idea of Smalltalk is that it's highly dynamic and live. You can try the thing you are asking about in just 2-5 minutes, and see if it works :)

Upvotes: 4

codefrau
codefrau

Reputation: 4623

Yes. Classes are just objects.

Upvotes: 4

Related Questions