How to understand Groovy's classes as first-class citizens

groovy

Solution

In Groovy and many other dynamic languages everything is an object, including class itself.

Say you have a class Circle in java. You need to call Circle.getClass() for an class object do deal with. In many dynamic languages, class itself does not need to be specified. Say you have a class

class Miu {}

and each later reference to Miu will be referencing to the class object itself

Miu.class
Miu

will both evaluate to the very same object

In other words, Java and earlier in C++ had no eval(), so class definition itself cannot be made into class object directly. OO model in them is more like class oriented programming rather than true object oriented, as classes are subtly not objects. In more recent interpreted dynamic languages classes themselves are directly objects.

Problem

I am an experienced Java developer, but a novice Groovy programmer, which I am learning at the moment (and it's great so far). As a reference I am reading this document: http://groovy.codehaus.org/Groovy+style+and+language+feature+guidelines+for+Java+developers That is all fine, except one thing I do not fully understand. The documentation says that classes are first-class citizens and the `.class` suffix is not needed in Groovy. But if that is the case how can I then refer to an object's type (i.e. class) in Groovy? Consider the following example: ``` def o = new Object() println("$o, ${o.class}") ``` Which gives me the following output: ``` java.lang.Object@da479dd, class java.lang.Object ``` This output is expected and makes sense. But what is the Groovy documentation than referring to when they say that the `.class` suffix is not needed?

Original source