Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Could a class be a first-class object?

If yes, how would the implementation look?

I mean, how could syntax for dynamically creating new classes look like?

EDIT: I mean what example syntax could look like (I'm sorry, English is not my native language), but still I believe this question makes sense - how you give this functionality while keeping language consistent.

For example how you create reference for new type. Do you make reference first-class object too and then use something like this:

Reference<newType> r = new Reference<newType>();
r.set(value);

Well this could get messy so you may just force user to use Object type references for dynamically created classes, but then you loose type-checking. I think creating concise syntax for this is interesting problem which solving could lead to better language design, maybe language which is metalanguage for itself (I wonder if this is possible).

share|improve this question
2  
You're going to have to do a dramatically better job of phrasing this if you want to save it from certain doom. –  user29776 Aug 17 '11 at 19:16
2  
@mrpyo: Asking how the syntax would look makes no sense, because syntax is by definition language-specific. If you are designing a language, I would suggest you to get the semantics right and only then start worrying about syntax. –  Eduardo León Aug 17 '11 at 19:40
1  
@kekekela: Classes as first class objects is a well defined term. Don't need to be much clearer than that. –  Loki Astari Aug 17 '11 at 21:27

7 Answers 7

up vote 7 down vote accepted

Ruby has this capability, along with a (perhaps too-) detailed API for modifying classes on the fly.

You can get a taste of this by looking through the documentation for the Ruby core classes Class and Module (Class is a subclass of Module, as they share some code for namespace management), and most Ruby books will have at least some coverage of this.

share|improve this answer

Different programming languages are designed according to different philosophies and with different kinds of users in mind. Some languages treat classes (or, more generally, types) as first class objects; other languages do not.

Languages that treat types as first-class objects can be divided in two groups:

  1. Those that treat types as immutable objects at runtime (i.e., you can query, but not modify the valid operations on a type, and the types of the parameters and return values of these operations)
  2. Those that treat types as regular objects at runtime (i.e., you can dynamically define and modify types and operations at runtime).

The latter are particularly powerful, although these capabilities usually come at a performance cost, because a runtime must take care of the types and operations you have dynamically defined.


Regarding how the implementation would look: Are you asking this as a language user or as a language designer?

To a language user, the only thing that matters is what I said in the previous part: a runtime must take care of the types and operations you have defined.

To a language designer, well, you have to design such runtime. Oh, and implement it, if you want your language to be used in the real world.


I do not see the point of the question regarding how the syntax would be. Syntax is, by definition, language-specific. If you are a language designer, I would suggest you to get the semantics right before worrying about syntax.

share|improve this answer

Sure, nothing simpler than having a class be a first class object:

Object subclass: #MyObject
    instanceVariableNames: ''
    classVariableNames: ''
    poolDictionaries: ''
    category: 'My-Category'

(Smalltalk.)

share|improve this answer
1  
Smalltalk was the first thing that came to my mind when I heard the question. –  Joachim Sauer May 10 '12 at 6:09

Eli Bendersky recently posted Python Metaclasses, which discusses that very question. In it, he starts with

In Python, everything is an object. And that includes classes. In fact, classes in Python are first-class objects – they can be created at runtime, passed as parameters and returned from functions, and assigned to variables.

He goes on to show some working Python examples of creating a new Metaclass, which can be used to create new classes. Highly worth reading.

share|improve this answer
    
Is the Metaclass class object (i.e., the object whose identity is that of the Metaclass class) modifiable itself? –  Eduardo León Oct 4 '11 at 17:10
    
If you mean the Metaclass that you constructed, yes - it's an object like any other. (At least you can modify it, by default - it is also possible to create an object/class that doesn't let you add new attributes). If you mean the underlying base class, which is type, then no, you can't modify the type object (as far as I can tell). –  Cyclops Oct 4 '11 at 19:17

Certainly.

A specific example: In a prototype-based approach, a class is effectively the same thing as an instance of a class, and it can just be cloned with new fields added or overwritten to make new instances.

A simple prototype-based example in Clojure:

(def human
  {:species-name "Human"
   :leg-count 2})

(def john-smith
  (merge human
    {:first-name "John"
     :last-name "Smith"
     :profession "Brewer"}))

(def john-silver
  (merge human
    {:first-name "John"
     :last-name "Silver"
     :leg-count 1
     :profession "Pirate"}))

(:leg-count human) ; class value
=> 2

(:leg-count john-smith) ; inherited value
=> 2

(:leg-count john-silver) ; overridden value
=> 1
share|improve this answer

Classes are objects in Objective-C, a feature which I think comes from Obj-C's Smalltalk roots. You generally don't create new "class objects" yourself in code; the compiler does that for you. That makes sense when you think about it: a class object is an object that happens to represent a class; if you instantiate it by sending it an +alloc message, you'd end up with a new object of the class represented by the class object, not another class object. That last bit really does make sense, I promise, but it's hard even for me to follow it, so here's some code to illustrate:

Class foo = [NSString class];        // foo points to the the object
                                     //    representing the NSString class
NSString *bar = [[foo alloc] init];  // bar points to an object of type NSString
int length = [bar length];           // length will be 0 because bar is empty

Note that both foo and bar are valid objects to which we can send messages.

It is actually possible to add class objects at runtime, but you do it by making calls to the Objective-C runtime directly.

share|improve this answer
2  
+1 for knowing your roots! –  Frank Shearar Aug 18 '11 at 8:33
1  
Your Class *foo should be Class foo instead. Class is a type that can hold pointers to class objects (similar to how id is a type that can hold pointers to objects). But Class is not a class and class objects are not "of type Class". –  user102008 Jun 1 '12 at 0:07
    
@user102008 Thanks for the correction. Updated. –  William Shakespeare Jun 1 '12 at 1:22

That's more or less the case in Ruby.

Object.const_set("Student", Class.new)

This is maybe the most simple way to demonstrate this feature. A class in Ruby is an instance of Class. With more code you can add member functions and variables.

Explained in more detail here

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.