Implicit conversions in Specs2 gone mad

Hidiho!

In this resurrection post I want to talk a little bit about a problem we faced recently at CarJump (how I ended up there is an story for another post) with Specs2 and Mockito. Actually, the issue I want to address is subtle and appeared in a very specific scenario. Lets start describing such scenario.

Disclaimer: the scenario below is valid for specs2 version 3.7.x – with specs2 2.x everything was fine. Implicit conversions defined by specs2 changed quite a bit between those two versions.

First, we have a specs2 test specification. Something as simple as the following:

class ImplicitsSpec extends Specification {
  "my spec" should {
    "do something" in {
      pending
    }
  }
}

This works just fine, it is just a simple specs2 Specification. Next comes adding mockito. In specs2, it is nothing more then adding the Mockito trait to the test suite. Or, and this is where it gets you, wherever you define your mocks. This is the pattern we started to used recently:

object ImplicitsSpec extends Mockito {
  // my common test vals here
}

Putting that code into words, we create a companion object that will hold all common vals used in the tests. We actually are starting to do this kind of thing in lots of places, and this is the first time it was a problem.

So, how are we to use those common values? Just import the companion object members. Applying this strategy to the spec presented earlier, the result would be something like:

class ImplicitsSpec extends Specification {
  import ImplicitsSpec._

  "my spec" should {
    "do something" in {
      pending
    }
  }
}

Pretty simple and nothing can go wrong there, right? Well… wrong. If you try to compile the code above, you will get an error like the following:

/src/test/scala/com/jcranky/specs2/implicits/ImplicitsSpec.scala:11: type mismatch;
[error]  found   : org.specs2.specification.core.Fragment
[error]  required: org.specs2.matcher.Matcher[String]
[error]     "do something" in {

Wait, what?

What happens is that the specs2 Mockito trait brings into context several other implicit conversions, not only mock related stuff. And what’s more, imported conversions have higher precedence over conversions got from class definitions. In our case, it means that whatever comes from

import ImplicitsSpec._

comes before what we get from extending Specification. In this case, in practice, we are losing a conversion from Fragment to Matcher. At this moment, I couldn’t find exactly where this conversion is, but there is a few workarounds to fix this. The first one is to change the companion object declaration to be:

object ImplicitsSpec extends Specification with Mockito

This will bring all relevant implicit conversions to the same scope. Another common solution would be to declare the test specification like below, and remote Mockito from the companion object:

class ImplicitsSpec extends Specification with Mockito

The problem with this solution is that then you cannot create common mock objects in the companion object. There are obviously other solutions, but they usually get more complicated. Still, if you know the root of the problem, please leave a comment!

What is the Best forum software out there?

And I’m back! It has been a long while since I last blogged, so lets get going right away!

What is the best forum software available out there?

Not the best question to start with, so the best answer it not optimal as well: it depends. Depends on what you want, on what you are looking for. What I want is something that:

  • is simple and easy to manage – I won’t have much time the manage stuff;
  • is free or at least with a good entry level pricing – I want to create a community, but it has no direct commercial goals;
  • is modern looking and easy to use.

Those are perhaps too much to ask, but lets find out.

The options

Bellow are the forum board software I found, some I already knew about, and others were recommend by friends. The grouping is not random: we will quickly analyse similar systems together.

These are the simpler ones. phpBB is probably amongst the better known of all software that I looked at, and both jforum and Simple Machines seems to be highly inspired by it. Both phpBB and jforum are open source, but I couldn’t really find that out about Simple Machines.

Those three got ruled out because of the third of my requirements: they are old-looking. They have a very dated UI, but could be a good option if you are looking for a well known formula.

Next.

Now things get visually much better. All three options above are modern looking and seem quite interesting. NodeBB and Discourse also bring something new to our comparison table: they both offer commercial / hosted solutions, which is great when you don’t have expertise or time to setup your own installation.

I discarded Mamute because it is not much more than a Stackoverflow clone, and I wanted something more community focused, and less Q & A focused. Discussion should be fine and encouraged for what I’m looking for.

NodeBB and Discourse were a different matter. I was really close to choose one of them when I found about the winner – more on that one later. Also, they both have a small problem for me: they are based on technologies I’m not totally familiar with, which means I would have to spend sometime learning. It would not be a big deal, but the winner is really a killer. Finally, the hosted version seemed a bit expensive to me.

Next.

A fully commercial option. Good looking and feature rich, lost me on the price point. They base their price on the number of online users – but how am I to know that, considering I’m just starting?

The winner.

Another commercial option. Two features really got me: first, it is embeddable. It can be part of your site, instead of a different or separated thing altogether. Second, it has a great starting price: Zero. You can use the free version for as long as you want, and if you decide to pay, the service levels are not based on the number of users you have, which makes everything that much simpler.

Also, the way Muut organizes information is exactly what I wanted: community focused. And to make things even better: the setup is ridiculously easy. Just drop in a html snippet in your site and you are done. They also have an API and other really cool features!

There are only two features I miss on Muut: sticky posts and locked posts. If the community is well behaved, this shouldn’t be a problem, but we will find out.

Finally….

What is it that I am doing, you might wonder… Well, I’ve been working on EasyForger (http://easyforger.com/) for a while now. It is starting to get some users, and I would like a way to connect them together. You can see our forums at forum.easyforger.com – it is only in Portuguese for now, though =].

An Amazon S3 Script, a Video and a question

Hi guys and gals! It’s been a long time since I last posted something. I’ll talk a bit about the reasons behind it in the end of this post, but first the main course: the S3 Script.

I had to write a script to fix some images I have in my small store (Lojinha), and the images are all uploaded to Amazon S3. So I figured I should do it in Scala, and also that I should create a video explaining how I did it. You can find the script in a gist here.

The video is here and, with it, I bring you a question. The video is in Portuguese – which obviously means that if you don’t speak this language, this is a problem. So the question: Are you interested in this content in English?

This is an important question, that goes back to my point in the first paragraph: the reason why I’m writing less here: I’m focusing a lot more on my Youtube channels (Dev, Games and Personal). Now, if you are a non-Portuguese speaker and if you tell me that you want to be able to understand the videos anyway, this might give me some energy to solve this problem.

The solution I have in mind is not creating whole new videos in English – that would be just too much work. But Youtube supports Closed Captions – which means I could just translate whatever I’m saying in Portuguese and add subs to the videos. Would that be compelling for you?

Thanks for sharing your opinions!

[]s,

Akka Essentials Book Review

Recently I had the opportunity to read the Akka Essentials book. This post is a brief review of this publication. Hope you enjoy.

The only good thing about this book is that it is short. Ok, that’s an exaggeration, but don’t expect too much from this book. To keep things in perspective, I read the kindle edition of this book this last month – so perhaps some of the problems mentioned below are specific to this version.

First thing I need to say about this book is that, a lot of times, it has at least awkward (to not say wrong) English sentences. It really could use some serious revision here. Some formatting review would also be good: in the “About the Reviewers” section there is even a whole duplicated paragraph.

The book covers lots of stuff about Akka – so it might be a good first glance at what this framework can do. Just don’t trust too much the code samples – more on that in a moment. Another interesting point, which might be good for some people, and bad for others, is that it presents examples both in Scala and Java.

In the sample code, lots of times, the author has slices of code that he uses to explain something. Than he goes on to add the entire code again, in a single unit. This is another point where I guess some people will like, and others won’t. I didn’t – it feels like wasting space that could be used to better explain some concepts.

Now to my pet peeve: a sample message, in an earlier chapter in the book. Try to guess what’s wrong in the code bellow (extracted from the book) before reading on:

import java.util.List;
public final class MapData {
  private final List<WordCount> dataList;
  public List<WordCount> getDataList() {
    return dataList;
  }
  public MapData(List<WordCount> dataList) {
    this.dataList = dataList;
  }
}

We have a class named MapData, whose objects are going to be used as messages to actors. It has a java.util.List attribute which is not guaranteed to be immutable, and no defensive copies are used. The problem with this is that such mutable messages can corrupt the actors, making them unreliable at all – this is even mentioned in the official Akka documentation, and is a serious error: http://doc.akka.io/docs/akka/2.0.5/java/untyped-actors.html – take a look at the “Messages and immutability” section.

In Scala, writing immutable messages is a lot easier, so at least there the author should have a correct message, right?  Unfortunately not. This is his Scala version for the message:

case class MapData(dataList: ArrayBuffer[WordCount])

Oh man… Even the Scala version of the message is wrong! ArrayBuffer is a mutable data structure. You really have to get out of your way to make this error. It is normal for people that are starting to use actors to write this kind of code, but I really expect more from a book trying to TEACH Akka. I wrote a bit about this subject here.

Another thing a bit strange or off-putting in the book is that sometimes it feels like the author is just throwing information at you, randomly. Also, a lot of times, the text says one thing about the code, and the code itself is a bit different from what is mentioned in the text – another clear lack of revision. In one example, when talking about routers for the first time, the author says we could pass messages in a round robin fashion using routers (which is correct). Then he goes on to show a sample code using a … BroadcastRouter! This kind of router has a completely different semantics.

All in all, since I love Akka, I would be happy to be able to recommend this book. The latter chapters even got better then the first ones. But I can’t. You are probably better off just reading the official documentation.

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.

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.

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.

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.

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 …. ?