A little bit about Scala eXchange 2012

Last week I went to Scala eXchange 2012, in London. The conference was great, and I’ll have more to say about it later, perhaps through other means different than here. Not sure yet. I’ll also talk about the event in the next Scaladores Meeting.

The first thing I want to say is that I love London, and it was really great to go there again, after a few years. So thank you Skills Matter for being in London!!

Now to the meat of the event. Scala. Two days full of Scala, both ending in beer and pizza. Can’t ask for much more. I’ll quickly mention a few talks I liked, together with some pictures. First off, the event was in a really charming location, as you can see in the picture bellow:

the crypt - scala exchange

the crypt – scala exchange

The conference started with Martin Odersky himself. He talked a lot about the progress of scala in general. First, from the point of view of his course in Coursera. Then, he proceeded to talk about the new features coming on scala 2.10.

Martin Odersky

Martin Odersky

There were several other talks during the day. One of them was the Enrik’s talk about the Typesafe console architecture, which was quite nice, and Spiewak’s talk about compilers. This last one was just… amazing. If you are curious, you can watch the video of the talk here, its really worth your time. Now, a couple more pictures:

Enrik on the Typesafe Console

Enrik on the Typesafe Console

Spiewak on Compilers

Spiewak on Compilers

The first day ended with an Akka code kata. I spent some time there to see how people “from the source” are teaching Akka. Interesting to see I’m doing it myself on a similar way =)

The second day was interesting as well, full of small speeches. I liked them in general. One was really good: Roland Kuhn’s talk about Akka. He talked about what is coming on the next Akka release, and it is promising.

Roland on Akka

Roland on Akka

Another one was very promising as well: Jon Pretty, talking about Rapture.io. This is an IO framework he is working on which sounds really interesting. He did lots of demos reading and writing data, always with simple and readable lines of code. I’m curious to see how far this project is going.

Jon on Rapture IO

Jon on Rapture IO

In summary, it was a long (and a bit expensive…) trip to get to Scala eXchange, but the whole experience was well worth the effort. I hope to return next year.

Posted in scala | Tagged , , , , , , , , | Leave a comment

What about the Scala community?

Here in Brazil, the scala community is still at its early stages. We are few, but we exist. And more people are always learning about the language, and some of them join the community. A friend and I even created a user group for in-person meetings, the Scaladores. We have almost a year now. And that is why I can say new people are getting to know Scala: we almost always have new faces in the meetings.

Now, how is the Scala community in your region? Europe in general, and in special London and Berlin seems quite nice in this regards. I’m always hearing about meetings and stuff in London, and will even be attending a conference there. Some places like the Bay Area in the USA also makes some noise. Where else?

I guess one of the things that shows how Scala is really growing, world wide, is the availability of courses and trainings, like the ones offered by Typesafe. And there are other companies doing this as well, like Dick Wall’s Escalate Software. So… now we have it in Brazil. In a partnership with Globalcode, I created a Scala course, to try to fill this gap.

Now the Scala ecosystem is more complete down here in São Paulo, Brazil. We have a user group, people using scala, new people learning (and loving) the language all the time, and now a training. What else is missing? What else do we need to help Scala grow even more in Brazil? Any comments, suggestions …. ?

Posted in scala | Tagged , , , , , , , | Leave a comment

Scala Exchange 2012

And so I decided. I will be attending Scala Exchange, at Skills Matter, in London, November 19th-20th.

I was looking for a good excuse to travel to London for a while, and this conference seems to be it. A good chance to meet nice people, and to visit a city I love.

Anyone out there is in for a beer or two?

Posted in scala | Tagged , , , , | 1 Comment

Defensive object copies in java and how scala avoids it

In one word: immutability. But read on for the whole history =)

In another, old post, I wrote about the usage a primitive long value instead of Date objects in attributes. This kind of thing is usually called defensive copy, i.e, we copy the value we received in a setter or constructor, instead of assigning it directly. We could have, also, copied the date as a new date object. This may sound simple, but wait until you have big objects, with complex hierarchies.

In theory, the clone method could help us solve this problem. I wrote about this method a long while ago: the bads of the java clone method, illustrating a bit how the java language sometimes makes us think the wrong way. The Effective Java book explain this problem also, so maybe you’ll want to check it out.

But that analysis on the clone method was by no means complete. More than what I mentioned previously, there is at least one more thing that is bad. Extracted from the javadoc:

By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by super.clone before returning it.

It means that, whatever it does, the clone method doesn’t return a real copy of the object, but a shallow one instead. And it goes as far as saying that you are responsible to make the copy right, by modifying fields… now imagine doing that with an object that references a couple of others, which by turn references others and others… exponentially difficult to get it right.

class A(b: B) extends Cloneable {
  override def clone() = super.clone
  override def toString() = "[A: %s]".format(b)
}

class B(c: C) extends Cloneable {
  override def clone() = super.clone
  override def toString() = "[B: %s]".format(c)
}

class C(var x: Int) extends Cloneable {
  override def clone() = super.clone
  override def toString() = "[C: %d]".format(x)
}

val c = new C(10)
val b = new B(c)
val a = new A(b)

val a2 = a.clone
c.x = -99

The sample code above is in Scala, but Java code would be very similar. So, is the code correct? When I do c.x = -99, should it affect only a, or both a and a2? Currently it does the later:

scala> println(a)
[A: [B: [C: -99]]]

scala> println(a2)
[A: [B: [C: -99]]]

So here is where the recursive cloning would take place. I would have to make the clone method inside A call the clone method from B, and so on. Not fun. The java language is not nice here, but perhaps the problem is deeper. Lets try to change our way of thinking.

After a few years in Scala land, I find there is something more to add. One thing that Scala tries to convince you is that you should have immutable objects as much as possible. That means that your objects never changes – you create new ones if need be. That also means that they can be shared easy and safely – thus clone is not necessary anymore.

Back to the sample code above, c.x = -99 would never be allowed – so it isn’t a problem at all. To make it happen, our new C class would look like this:

class C(x: Int)

Removing the var keyword effectively makes x immutable. What if you really want a new x? You have to create a whole new A. If this class had more fields, it could be boring, so scala helps us here. Lets change our classes to be immutable, forget about cloning stuff, and to be case classes. Lets also add another attribute to A.

case class A(b: B, name: String)
case class B(c: C)
case class C(x: Int)

val c = C(10)
val b = B(c)
val a = A(b, "jcranky")

First, being case classes, we don’t need to bother with the toString overrides anymore. Nor with the new operator. And we also got a prize: the copy function. With this, we can copy an object and change only what we want. This is a sample execution of out new classes:

scala> println(a)
A(B(C(10)),jcranky)

val a2 = a.copy(b = B(C(-99))

scala> println(a)
A(B(C(10)),jcranky)

scala> println(a2)
A(B(C(-99)),jcranky)

The only drawback is that you have to design your application with this kind of structure in mind. From that on, only bonuses. You don’t have too much to worry with locking for example, since there is no mutable stable to watch over.

Posted in java, scala | Tagged , , , , , , , , , | 1 Comment

akka microkernel with maven

And then I have to use the Akka Microkernel.

Not that this is bad news. The bad news is that I have to do it with maven. The project I’m doing this for is already using maven, and has a few modules – which makes migrating to sbt out of question. Akka has a nice sbt plugin that generates the package for a microkernel distribution of your akka project – but there is no such thing for maven.

How to solve this problem with maven? Fortunately, it isn’t that hard. There are probably lots of other ways to do it, but my solution will use the maven-assembly-plugin. The boring part, as always with maven, is to write the proper xml. I’ve created a new assembly descriptor, called akka.xml, that goes as follows:

<assembly xmlns="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/plugins/maven-assembly-plugin/assembly/1.1.2 http://maven.apache.org/xsd/assembly-1.1.2.xsd">
  <id>akka</id>
  <formats>
    <format>zip</format>
  </formats>
  <fileSets>
    <fileSet>
      <directory>${project.build.directory}</directory>
      <outputDirectory>/deploy</outputDirectory>
      <includes>
        <include>*.jar</include>
      </includes>
    </fileSet>
  </fileSets>
  <dependencySets>
    <dependencySet>
      <outputDirectory>/lib</outputDirectory>
    </dependencySet>
  </dependencySets>
  <files>
    <file>
      <source>src/main/resources/akka/start</source>
      <outputDirectory>/bin</outputDirectory>
    </file>
    <file>
      <source>src/main/resources/akka/application.conf</source>
      <outputDirectory>/config</outputDirectory>
    </file>
  </files>
</assembly>

Two points to notice: the xml assumes an akka start script at src/main/resources/akka/start and a configuration file at src/main/resources/akka/application.conf, so you will have to create those. For me, I just copied the default start generated by the akka sbt plugin and created an empty application.conf, and will tweak them later.

Now, to enable the plugin add the following to the plugins section of your pom.xml:

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <version>2.3</version>
  <configuration>
    <descriptors>
      <descriptor>src/main/assembly/akka.xml</descriptor>
    </descriptors>
  </configuration>
  <executions>
    <execution>
      <id>make-assembly</id>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
</plugin>

This one also assumes something: the presence of the previous xml code, in the folder src/main/assembly/akka.xml.

That should do it! Now just unpack the zip file in your server and run your Bootable akka code =]

 

EDIT:

Since the xml mentioned above went through small tweaks, I added them in this GIST. So if you want to see the most recent version, go there 😉

Posted in scala | Tagged , , , , , , , , , , | 13 Comments

TDC 2012 and Scala

 The Developers Conference 2012, um evento organizado pela Globalcode

From 4th to 8th July this year we will have the TDC conference in São Paulo. Although we are already very close to the event date, the call for papers is still open, so please submit something. Of special interested to this blog’s readers might be the fact that this year there will be an Scala track!

The conference last year was very nice (I wrote about it here) and this year it promises to be even better. There are technologies for all kinds of people, so no excuse!

Also, there is another way you can participate: helping to promote the event! This page better describes how it works.

See you all there!!

Posted in java, scala | Tagged , , , , | Leave a comment

a small store application in scala, with play and akka

I’ve been promising some people that I would put a personal project I’m working on available on github. I wanted to have it done and usable before doing that, but since I always get delayed by something, I decided to make the source code available right away. So I did this last week and you can find it here. It is called “lojinha”, which stands for something like “small store” in Portuguese.

The goal of the project is to implement a personal auction system, so that I can sell my used stuff I don’t want anymore. As of now, the project uses Play Framework 2.0 and Akka. Slow tasks like image thumb generation are delegated to akka actors. Also, the whole project is written in Scala, my current language of heart.

I’m still choosing where I’m going to deploy my instance when the project is ready, but I’ll publish a link here when it happens, so that you can buy something if you want =). It will probably be Amazen EC2 or Heroku.

Finally, all feedback is welcome!

Posted in scala | Tagged , , , , , | 5 Comments