Discussion:
Smalltalk metaclasses
(too old to reply)
a***@gmail.com
2013-08-08 05:24:46 UTC
Permalink
Hi, I'm very much a smalltalk newbie and I have a question about metaclasses.

I'm still coming to grips with smalltalk terminology, especially since I do a lot of Ruby, so I see a lot of similarities, but I also see some things that confuse me because I'm probably thinking more in terms of Ruby.

As I understand, each class is an instance of a metaclass, am I correct there? And this is where Class methods would be defined, as opposed to instance methods on the class itself?

So my confusion is with objects that are instances of classes. Do objects also have a metaclass (similar to singleton or eigenclasses in Ruby?) If so, how do you define methods on an object's metaclass? Or are metaclasses are only the parent of a class? Is there any concept of eigenclasses on object instances like Ruby?

Thanks.
Louis LaBrunda
2013-08-08 14:20:30 UTC
Permalink
Hi Adam,

I don't know Ruby and my knowledge of Smalltalk comes from years of using
it and not from formally studying it, so my use of terminology may not be
perfect but I will try to help anyway.
Post by a***@gmail.com
Hi, I'm very much a smalltalk newbie and I have a question about metaclasses.
I'm still coming to grips with smalltalk terminology, especially since I do a lot of Ruby, so I see a lot of similarities, but I also see some things that confuse me because I'm probably thinking more in terms of Ruby.
As I understand, each class is an instance of a metaclass, am I correct there? And this is where Class methods would be defined, as opposed to instance methods on the class itself?
In Smalltalk the term metaclass is used to describe a parent class that
usually doesn't get instantiated. Meaning it has sub classes and they get
instantiated.
Post by a***@gmail.com
So my confusion is with objects that are instances of classes. Do objects also have a metaclass (similar to singleton or eigenclasses in Ruby?) If so, how do you define methods on an object's metaclass? Or are metaclasses are only the parent of a class? Is there any concept of eigenclasses on object instances like Ruby?
In Smalltalk everything is an object. When you define a new class you are
in a sense defining two things, the class factory and the class the factory
makes. This is pretty much invisible to you as it is all housed together
and you do it all at once (obviously you add methods, both class and
instance over time). The class factory is a singleton but you don't really
need to worry about it.

There are class methods/variables and instance methods/variables. You
define a new class with one of the browsers and it helps you work with the
class or instance side.

You instantiate an instance of your class with the #new method (a class
side method) like so:

myInstance := MyNewClass new.

You don't need to define the #new method unless you need to do something
special as a parent class factory will already have it.

I have used the term "class factory" all lot normally we just speak of the
"class" which is the definition of the class and "instance" which is an
instance of a class.

I hope this helps.

Lou
-----------------------------------------------------------
Louis LaBrunda
Keystone Software Corp.
SkypeMe callto://PhotonDemon
mailto:***@Keystone-Software.com http://www.Keystone-Software.com
r***@gemtalksystems.com
2013-08-08 23:33:07 UTC
Permalink
Post by a***@gmail.com
Hi, I'm very much a smalltalk newbie and I have a question about metaclasses.
I'm still coming to grips with smalltalk terminology, especially since I
do a lot of Ruby, so I see a lot of similarities, but I also see some things
that confuse me because I'm probably thinking more in terms of Ruby.
Stephane Ducasse maintains a list of free online books related to Smalltalk. The original authoritative reference is called the "blue book" (because of its cover art).

Smalltalk-80: The Language and its Implementation By Adele Goldberg and DavidRobson; Xerox Palo Alto Research Center ISBN 0-201-11371-6. 344 pp. 1983
http://stephane.ducasse.free.fr/FreeBooks/BlueBook/

Chapter 5 explains metaclasses.


The introduction to the chapter says every object is an instance of some class. "A class whose instances are themselves classes is called a metaclass."

"... each class [is] am instance of its own metaclass", allowing classes to have different behaviours.

"Metaclasses are similar to other classes because they contain the methods used by their instances. Metaclasses are different from other classes because they are not themselves instances of a metaclass. Instead, they are all instances of a class called Metaclass."


The circularity of this portion of the Smalltalk object model causes a lot of confusion, even though it is rigorously simple. It's just hard to visualize!
Post by a***@gmail.com
As I understand, each class is an instance of a metaclass, am I correct
there? And this is where Class methods would be defined, as opposed to
instance methods on the class itself?
100% correct.
Post by a***@gmail.com
So my confusion is with objects that are instances of classes. Do objects
also have a metaclass (similar to singleton or eigenclasses in Ruby?)
No. Only classes have a metaclass (by definition, see above).

A singleton is an object which is constrained to be the only instance of its class. (I can't speak to Ruby terminology.)
Post by a***@gmail.com
[...] Is there any concept of eigenclasses on object instances like Ruby?
I have no idea. http://madebydna.com/all/code/2011/06/24/eigenclasses-demystified.html mentions "singleton methods" which are attached to an object and yet separate from its class.

In Smalltalk, this kind of thing is occasionally done, but by creating a new class on the fly with the specific additional behaviour and then instantiating that class.
Loading...