Talk:Metaclass

From Wikipedia, the free encyclopedia

Contents

[edit] java.lang.Class

Is java.lang.Class a metaclass? —Preceding unsigned comment added by Wlievens (talkcontribs)

Not really; that's more related to introspection. It doesn't define how classes and their instances behave. All classes and instances behave the same in Java: they do what the JVM says. Java doesn't really have metaclassing. Jorend 16:12, 16 February 2006 (UTC)
The above answer is not correct. The definition is "a metaclass is a class whose instances are classes." In Java, all classes including java.lang.Class are instances of java.lang.Class. A good reference on this subject is Appendix A of Java Reflection in Action by Ira R. Forman and Nate Forman, Manning, 2005. The issue alluded to above is that Java Reflection API does not allow one to dynamically create classes or modify them.--Ira Forman 15:32, 20 December 2006 (UTC)
Calling a java.lang.Class object a class strikes me as a little weird, though. My view is that Class objects merely represent classes; they're not really classes. This view is supported by the docs. For example, the only place I can find java.lang.Class mentioned in the Java Language Specification [1] is here: "The method getClass returns the Class object that represents the class of the object." (Emphasis added.) Similarly, the javadoc for java.lang.Class says: "Instances of the class Class represent classes and interfaces in a running Java application. Every array also belongs to a class that is reflected as a Class object that is shared by all arrays with the same element type and number of dimensions." (Emphasis added again.) And there are Class objects, especially Void.TYPE [2], which I would argue clearly aren't classes. --Jorend 14:27, 7 January 2007 (UTC)
No, Jorend is correct. You cannot instantiate java.lang.Class into a class (as required by your definition sentence), let alone extend it (even if it were declared non-final; there simply isn't any metaclass functionality to extend). It is a tool for reflection, nothing more. --Piet Delport 11:02, 16 January 2007 (UTC)
Gentleman, the definition of a metaclass is: "a metaclass is a class whose instances are classes." java.lang.Class meets this definition; consequently, it is a metaclass. Now there are two issues in your comments to discuss.
First, as Piet observed, one cannot instantiate java.lang.Class. This implies a huge weakness in the Java Metaobject Protocol. Programmers compensate for this weakness with many complex tricks played in specialized class loaders. If the Java MOP were sufficiently capable, such tricks would be unnecessary. However, this does not change the fact that by definition java.lang.Class is a metaclass.
Second, the definition of metaclass is part of a theory of reflection (a subset of the theory of programming languages) that is independent of specification of any particular programming language. The reason the JLS uses the word "represents" is that a Java class is a syntactic form written in the Java language. A class object is a representation of that syntactic form (and implies much about the meaning of that form). This sublty in wording of the JLS is required for languages that have reflection (i.e., where the semantics of the language is wholy or partially represected in the language itself). This issue has no bearing on the fact that java.lang.Class meets the definition of a metaclass.
BTW my home page (http://home.austin.rr.com/forman/ira/index.html) has two papers on JEM, my proposal to extend Java give java.lang.Class that capabilities that we all would like it to have.
Ira Forman 18:48, 17 January 2007 (UTC)
Ira wrote: "Gentleman, the definition of a metaclass is: 'a metaclass is a class whose instances are classes.' java.lang.Class meets this definition; consequently, it is a metaclass."
I accept the definition; I wrote it. Where your argument falls down is that the instances of java.lang.Class aren't classes.
Ira wrote: "The reason the JLS uses the word 'represents' is that a Java class is a syntactic form written in the Java language."
Nonsense. JLS consistently calls the syntactic form a "class declaration". --Jorend 16:14, 22 January 2007 (UTC)

[edit] See Also

The See Also list is larger than the article. Can't it be trimmed? -- Mikeblas 03:37, 27 January 2006 (UTC)

Done. Next time, Wikipedia:Be bold! Jorend 17:53, 16 February 2006 (UTC)

[edit] Squeak freezes on double Metaclass instantiation

Just try to do this:

Transcript show: (Metaclass new new).

Squeak goes irresponsive and takes 100% CPU usage. Could someone please explain why this happens?

'cause the Metaclass has no class. Try comp.lang.smalltalk to get a fuller answer. The
actual interaction between the VM and OS causing the error is beyond the scope of this ::article.Mal4mac 18:39, 13 December 2006 (UTC)

[edit] Ruby?

As far as i can tell, the "Metaclasses in Ruby" reference does not actually describe metaclasses, but a different Ruby concept known as "singleton classes". Any objections if i go ahead and remove it? --Piet Delport 09:58, 12 January 2007 (UTC)

OK, i've removed it. --Piet Delport 10:20, 16 January 2007 (UTC)

Ruby has the Class class and every classes are instances of it[3] so I'm going to add it again. --Kusunose 10:34, 6 June 2007 (UTC)

That doesn't provide metaclass functionality: you can't subclass or otherwise extend it. (See the discussion about java.lang.Class above.) --Piet Delport 20:01, 6 June 2007 (UTC)

(sigh) Someone re-added Ruby to the list, with a comment about Ruby's metaclasses being the same as Smalltalk's. I find that pretty unlikely. If it can't be sourced, I'm going to remove it again. —Jorend (talk) 13:42, 2 April 2008 (UTC)