Home > Scala > poking Unit with a stick (Scala)

poking Unit with a stick (Scala)

Scala has a thingy called Unit. For those coming from Java, Unit is similar to void but it is an actual type.

By my understanding, there’s only one actual instance of Unit ever and its value is ().

If you go to the Scala interpreter (type scala in a command prompt if you’ve downloaded Scala) you can assign Unit to a variable… er, um value in a few different ways.

One way is to simply create a value and assign () to it:
scala> val x = ()
x: Unit = ()

Another way is to make a value typed as Unit. You’re allowed to assign anything to it because pretty much any object in Scala can be automatically converted into Unit:
scala> val y: Unit = “turn me into Unit”
y: Unit = ()

I suppose the automatic conversion is because Scala methods return the value of the last expression. If Scala didn’t do this trick, we’d have to end every Unit-returning method with a gratuitous ().

Yet another way to end up with a Unit is to create a method without an assignment:
scala> def z(){“turn me into Unit too”}
z: Unit = ()

What’s this I say about a method without an assignment? Well the method z() I wrote above looks like a normal method to many folks (especially Groovy people). However, in Scala, most methods are created with an equals.
scala> def a() = “hello”
a: ()java.lang.String

Skipping the = is like automatically typing the method return as Unit, as if the z() method above were written as:
scala> def z(): Unit = {“turn me into Unit again”}
z: ()Unit

One weird thing I found with Unit is if I try to call getClass() on it:
scala> ().getClass
res1: java.lang.Class[_] = class scala.Predef$$anon$1

So it appears (), or Unit, is an anonymous inner class of something called object Predef.

Predef is a neato object. It pretty much is a way to get what appears to be a bunch of keywords and methods built into the Scala language, like println() and assertions.

Looking through the Predef source code, after being quite tickled by the ASCII art in the Scaladoc, I found the answer to another question I had about Unit – I had seen once in the Artima book unit with the lower case ‘u’. I originally thought it was a typo but tried it out in code and it seemed interchangeable. Line 36 of Predef, though the line is deprecated explains where unit (lower case ‘u’) comes from.

This was my experience digging into Unit in Scala. Try it out yourself and see what you can discover. Maybe you can uncover why ().getClass returns that weird Predef$$anon$1 thing.

  1. Jorge Ortiz
    December 23, 2008 at 4:14 am

    Unit is a subclass of AnyVal, which doesn’t actually have a getClass method. However, Predef has an implicit conversion for Unit to Ordered[Unit]. Ordered[T] is a subclass of AnyRef, which does have a getClass method. So calling getClass on () converts () to an anonymous class defined in Predef.scala which instantiates an Ordered[Unit].

  2. December 23, 2008 at 10:13 am

    Now I see on line 196 the unit2ordered implicit conversion.

    This got me curious to try another of the AnyVal types.

    scala> 1.getClass
    …Note that implicit conversions are not applicable because they are ambiguous: both method int2Integer in object Predef of type (Int)java.lang.Integer and method intWrapper in object Predef of type (Int)scala.runtime.RichInt are possible conversion functions from Int to ?{val getClass: ?}

    I interpret this to mean that there are two possible implicit conversions available and both have a getClass() method, creating a conflict because Scala has no clue which of the two conversions to choose.

    If I force a conversion before calling getClass, I have no problem.
    scala> val x: scala.runtime.RichInt = 1
    x: scala.runtime.RichInt = 1
    scala> x.getClass
    res1: java.lang.Class[_] = class scala.runtime.RichInt

    Studying the source for Predef reveals a lot about how Scala works.

    Thanks for the tip, Jorge!

  3. December 23, 2008 at 10:20 pm

    Awesome title! This post makes me want to learn Scala again – all this type stuff is really interesting to me. I can’t wait to hear about it at CodeMash. Oh – and I’m a big fan of the ASCII art in the Scaladoc, too.

  4. May 28, 2010 at 7:52 am

    If only I had a dollar for each time I came here… Great writing.

  5. May 29, 2010 at 6:49 pm

    Really interesting post! Truely..

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: