NetBeans and Scala Screencasts

Last week I started recording screencasts. It’s a new experience, and I’m enjoying it. For now, its only in portuguese, but if you are english speaking and would like to watch them, I’ll consider recording in english as well. Just let me know.

I have two screencasts recorded and published so far: one about JSF with Scala, and another one about using NetBeans for Scala development. If you understand portuguese, please take a look and let me know what you think!

The screencasts can be found in my Youtube channel here.

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

Lojinha (small store) is in production!

Hi!

In this (a bit old) post, I mentioned an application I’m developing, called Lojinha – which stands for “small store” in portuguese. This development has two goals: exercise and explore some technologies I’m studying and using, and to sell (for real) some old stuff I have at home that I don’t use or want anymore.

I completely forgot to mention it here (which is the reason for this post), but Lojinha is now in production… for about a couple of months. You can find it here. It’s really refreshing to have it out there. It is a very simple application, yet it took me a while to find the time to put into it.

If you are interested in anything I’m selling, just post a bid (a fair one please), or contact me directly – either way is fine.

If you don’t care about my stuff, but are curious about how I developed that, the source code is available in my github. The application uses Play Framework 2.0 and Akka 2.0 – nice stuff!

Like I mentioned in the original post, I wanted to deploy the application in some cloud provider – and the one I was more familiar with won: Amazon EC2. It is running on a micro instance, and so far it is really working well.

Any feedback is welcome, both about the code and about the store itself.

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

How to write a desktop / standalone application in Scala?

An student asked this question to me a while ago, and I figured it might be valid for other people starting with Scala as well. So I’ll try to give general directions in this post.

The idea is not to write a full tutorial; instead, I’ll show you the possible paths you have.

First, the basics. In Java, when we want to have a standalone application, we have an structure similar to the following:

public class Main {
  public static void main(String args[]) {
    System.out.println("Hello World!");
  }
}

Since Scala also runs on top of the Java Virtual Machine, the structure will  be similar, with a few tweaks. The first one is related to the fact that Scala doesn’t have static fields, so we will have to use an object.

The second tweak is that we won’t even need the main method – we will use an utility class that have that method and it will call the code we pass to the object constructor. This is what the result looks like:

object Main extends App {
  println("Hello World!")
}

In practical terms, both versions will work the same way – but as always, the Scala version will be much simpler. We could also directly implement the main method in the Scala version, instead of extending App, but I’ll left that as an exercise to the reader.

The next question could be: how to write user interfaces with Scala?

The answer to this question is very direct as well: the same way we would write UIs in Java. For example, the following code uses Java Swing to create a simple window application:

import javax.swing._

object Main extends App {
  val frame = new JFrame("Hello World!")
  frame.setSize(300, 300)
  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
  frame.setVisible(true)
}

The code above is a direct translation from what a Java version would be. It leaves a bit to be desired, so if you want more, you can take a look at Scala Swing – which is a set of wrappers on top of the Java Swing API, to make it easier to use.

Another option is to follow the new trend in Java land: Java FX. You can use Java FX directly from Scala, or use a set of wrappers called Scala FX – whatever suits you better.

I hope I made it clear how you could start coding a desktop and / or standalone application in Scala. If not, please let me know.

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

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