0

There are quite a few dynamically typed object oriented languages in which a class itself is an object. Smalltalk, and Python for example. Is there any statically typed language in which a class is an object?

EDIT:
By the term "object", I mean a first class entity. For example, classes in Python can be passed to other methods, can be returned from methods etc.

missingfaktor
  • 90,905
  • 62
  • 285
  • 365
  • 2
    Either the answer is 'all', or I really don't understand your question. There are people who say that an object is an instance of a class. Others say that object and class are synonyms and thus you create an object/class *instance* to work with. What is you definition? – GolezTrol Jul 05 '11 at 07:25
  • unless im missing something..C#? Java? the list goes on.. – RPM1984 Jul 05 '11 at 07:25
  • @GolezTrol: No, class and object are not synonyms. I don't know where you came across that fallacy. – missingfaktor Jul 05 '11 at 09:21
  • @BoltClock: No idea about Objective-C. – missingfaktor Jul 05 '11 at 09:21
  • 1
    @RPM1984: In Java, `String` itself is not an object. The expression `String.class` however returns an object of type `Class<>` corresponding to the `String` class. – missingfaktor Jul 05 '11 at 09:22
  • @RPM1984: In Python, you can write a function that accepts a class object as its input and adds new attributes to it. – missingfaktor Jul 05 '11 at 09:26
  • So would something like [`System.Type`](http://msdn.microsoft.com/en-us/library/system.type.aspx) in the .NET Framework meet your requirements? I'm not entirely clear on what you're looking for, although I do understand the distinction you're making between classes and instances of classes. – Cody Gray - on strike Jul 05 '11 at 09:37
  • @missingfaktor - well obviously im misunderstanding your question. If i create a non-static class called "Car" in C#, i can instansiate it, and therefore it's an object. – RPM1984 Jul 05 '11 at 23:16
  • @RPM1984: You can instantiate `Car` means that `Car` is an instantiable type, a concrete class. It doesn't mean that `Car` is an object. What you get as a result of instantiation though is an object. – missingfaktor Jul 06 '11 at 04:29
  • @Downvoter: Care to explain?! – missingfaktor Sep 28 '11 at 07:11

5 Answers5

2

In a lot of statically typed languages, like JAVA, a class is an object with its own methods. For example, in Java, the object that represents the "String" class is accessible as "String.class" and then you can invoke methods on this class as "String.class.getFields()", "getMethods()", getConstructor()", "cast(obj)", etc. You can see in the API documentation all the methods of the "Class" class.

Nevertheless, given that the language is statically typed, you cannot dynamically modify a class.

In other words, you are not going to find a method called "class.addField()" in the Class class. The more you can do is "extend" the class (if it is not final) by inheritance.

In yet other words, the a "Class" object is read-only.

By the term "object", I mean a first class entity. For example, classes in Python can be passed to other methods, can be returned from methods etc.

As any other object, you can pass them to methods and return them from methods (they are just regular objects, but that expose only "read" methods). Example (I omit generics for clearness):

public Class myMethod(Class someClassObject) {
    System.out.println(someClassObject.getCanonicalName());
    Class anotherClass = String.class;
    return anotherClass ;
}
Mr.Eddart
  • 10,050
  • 13
  • 49
  • 77
  • See my response to @RPM1884's comment under the question. – missingfaktor Jul 05 '11 at 09:26
  • I have edited my answer. And regarding your comment, String.class ***is** an object*, specifically it is an instance of Class class. – Mr.Eddart Jul 05 '11 at 09:44
  • +1 In static languages you can do a lot with classes but usually not change them. Although it can be done through some hacking. I found a way to change the ancestor of a Delphi class in runtime. But indeed, there are no convenient methods to do so. – GolezTrol Jul 05 '11 at 10:37
  • I dare to disagree, you can see my answer why. – ewernli Jul 06 '11 at 13:20
2

I don't fully agree with @edutesoy answer.

First-class means that an implicit constructs is reified as an explicit construct that can be passed around like any object. Classes in Java are not "first-class", they are mirrors where you can inspect some properties but are not the the object itself.

That you can not change the structure of the class at run-time is fine, e.g. add fields, change method body or signature, and that under this perspective it's partly "read-only" is ok.

But let's consider static fields. I guess everybody agrees that unless final static fields are mutable, just like instance fields. In Smalltalk, these are just fields defined on the class itself, rather than on instances of the class. Also, in Smalltalk, class-side methods are polymorphic just like any other method, and :

aClass field: aValue. 

might resolve differently depending on the class that is passed. This is not possible in Java ; a static field or method must be referenced via its type. This also means that it does not allow overriding static methods (read the link for more details) as would be the case if they were truely first class.

The fact that reflection is possible in Java doesn't make classes "first-class"--you can only pass a representation of the class around. And to come back to the original question: I don't know of any statically typed language with first-class classes (but my knowledge is limited, maybe one exists).

EDIT

Actually, now I remember it exists StrongTalk (http://www.strongtalk.org/) which is Smalltalk with static typing. The typing issues are discussed in this paper: Strongtalk: Typechecking Smalltalk in a Production Environment

Community
  • 1
  • 1
ewernli
  • 38,045
  • 5
  • 92
  • 123
  • Interesting, I didn't know, but why a Java Class class instance is not a first-class object? I think that it meets the 5 requirements claimed in your wikipedia reference. – Mr.Eddart Jul 06 '11 at 15:12
  • 1
    @edutesoy What is first-class is indeed a bit fuzzy notion. But 1) `String` itself is not an object, only `String.class` is 2) static method are not bound to a class object and hence not polymorphic 3) classes can be created only via classloaders (point #4 of the definition). That `String.class` is an object is clear, but the reflective treatment of classes as object is not as first-class as, say, in Smalltalk. – ewernli Jul 07 '11 at 11:48
1

From Oleg Kiselyov and Ralph Lammel's "Haskell's overlooked object system" (emphasis mine),

Not only OOHaskell provides the conventional OO idioms; we have also language-engineered several features that are either bleeding-edge or unattainable in mainstream OO languages: for example, first-class classes and class closures; statically type-checked collection classes with bounded polymorphism of implicit collection arguments; multiple inheritance with user-controlled sharing; safe co-variant argument subtyping.

missingfaktor
  • 90,905
  • 62
  • 285
  • 365
0

Well, the benefits of that are reduced in an early-bound language. However, Java reflection objects for classes would basically be what you are asking for.

Why, incidentally, do you want this? Is it more than idle curiousity?

Marcin
  • 48,559
  • 18
  • 128
  • 201
0

Take a look at the concept of homoiconicity which refers to the extant that a language can refer to its own structures. Also take a look at this post by Eric Lippert.

eulerfx
  • 36,769
  • 7
  • 61
  • 83