Seeking anonymity. Anonymous classes

What are anonymous classes?

As their name already suggests, anonymous classes are unnamed classes, classes that don’t have a name. They are the classes that corrupt people like the most. They are not declared… BA DUM, TSS!

Now seriously, how can I create an instance from an anonymous class? Easy, simply by using the reserved word new and defining the body with braces.

val myPoint = new{ val x = 1; val y = 2 }

This way, we create an instance that has two integer values: x and y. However, as can be appreciated, we have given no name to this class.

tGWoYYo

Syntactic sugar

A few weeks ago, we talked about traits and how we could create an instance from a trait by means of an anonymous class.
Perhaps at that time, a WTF the size of Sebastopol crossed your mind. If we take a look at that post, the syntax for creating an instance from a trait was something like this:

  trait AnonymousHero {
    def superpower: String
  }

  val myHero = new AnonymousHero {
    def superpower = "I can compile Scala with my brain"
  }

We are creating an instance from a trait! They are no classes anywhere! Is this black magic? Well, no, it’s sintactic sugar.

Actually, what is really going on underneath is something like this:

  class AnonymousHeroClass extends AnonymousHero {
    def superpower: String = "I can compile Scala with my brain"
  }

  val myHero = new AnonymousHeroClass

As can be seen, when instantiating a trait, what is really happening is that a class that extends that trait is created. After that, an instance of that class is created.

This way, instances can be created from traits without any boilerplate code.

Bonus-track: Anonymous functions

Lambda expressions can be used in Scala, that is, anonymous functions. Further on, in other posts we’ll get to the importance of the use of anonymous functions in, for instance, methods that accept functions as parameter.
Anonymous functions, just like anonymous classes, are functions that don’t need to be declared.
This is an example of anonymous function:

  (x: Int) => x + 1

In this case, the anonymous function expects an integer and returns that same integer plus one unit.

We must not forget that, in Scala, functions are objects in reality. By taking this last consideration into account, we can relate anonymous functions (or lambda expressions) to anonymous classes.

So, when we use the anonymous function (x: Int) => x + 1, what is really happening is the creation of an instance of an anonymous class from the trait Function1:

new Function1[Int, Int] {
  def apply(x: Int): Int = x + 1
}

Everything fits!

tGWoYYo

Anuncios

One thought on “Seeking anonymity. Anonymous classes

Responder

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s