background preloader

Scala

Facebook Twitter

Scala Tests with IntelliJ Idea, Maven and ScalaTest. Why you are reading this: you want to be able to run unit tests in IntelliJ for your Scala project, using either the built-in support for Scalatest and/or Maven goals. The tools discussed are version sensitive so be careful… Putting together a Scala 2.9.1 project in IntelliJ Idea 11.1.1 to use Maven 3.0.4 and Scalatest 1.7.1 was painful. I felt like I was spinning my wheels forever while trying to set this up properly and it still feels quite kludgey. I’m new to these tools, so I hope that their poor integration is simply due to my inexperience. The challenges I faced seem simple now but hindsight is 20/20… Anyways let’s get it on. I’m going to go through things I did and problems I ran into chronologically. 3. The dependencies section between lines 11-29 tell Maven what to include in our project, this is a very basic implementation using only a tiny fraction of the settings available. 4. 5.

Fig. 1: InteliJ built in ScalaTest Result Fig. 2: Intellij ScalaTest Plugin Button That’s it! ScalaTest. Testing Akka with Specs2. This year I have been working on several systems based on Akka, usually in combination with the excellent Spray framework to build fully asynchronous actor-driven REST servers. All in all this has been going very well but recently I had to reinvent a certain wheel for the third time (on the third project) so I thought it might be good to blog about it so others can benefit from it too. The problem is very simple: Akka has great unit test support but unfortunately (for me) that support is based on ScalaTest. Now there is nothing wrong with ScalaTest but personally I prefer to write my tests using Specs2 and it turns out that mixing Akka TestKit with Specs2 is a little tricky.

The Akka documentation does mention these problems and it gives a brief overview of ways to work around them, but I could not find any current example code online. Based on that original example, here is my slightly improved and commented version: Scala School. Other Languages: About Scala school started as a series of lectures at Twitter to prepare experienced engineers to be productive Scala programmers. Scala is a relatively new language, but draws on many familiar concepts. Thus, these lectures assumed the audience knew the concepts and showed how to use them in Scala. We found this an effective way of getting new engineers up to speed quickly. Approach We think it makes the most sense to approach teaching Scala not as if it were an improved Java but instead as a new language. Most of the lessons require no software other than a Scala REPL. Also You can learn more elsewhere:

Learning Scala in small bites. Learning with the REPL One of the useful features for learning Scala is its REPL (read-eval-print-loop) support. If you want to try something out in Scala, just run: % scala and then try it out at the prompt. Examples Each well-commented script below demonstrates a different facet of Scala. [Values.scala] #! /* Scala has a rich set of value types, and a rich literal syntax to support them // Integers:val anInt = 3 // Floating point:val aDouble = 4.0 // Charaters:val aCharacter = 'c' // Strings:val aString = "Google" // Symbols:val aSymbol = 'foo // XML:val anXMLElement = <a href=" // Tuples:val aPair = (aString,aDouble) // Lists:val aList = List(1,2,3,4) // Ranges:val aRange = 1 to 5 // Maps:val aMap = Map(3 -> "foo", 4 -> "bar") // Sets:val aSet = Set(8,9,10) // Arrays:val anArray = Array(1,2,3,5) // Unit:val unit = () // Null:val nullValue = null // Functions:def incImplicit(x : Int ) = x + 1 val incAnonymous = ((x : Int) => x + 1) [Functions.scala] // f(x) => f.apply(x)

Effective Scala. Table of Contents Other languages 日本語Русский简体中文 Introduction Scala is one of the main application programming languages used at Twitter. Scala provides many tools that enable succinct expression. Above all, program in Scala. This is not an introduction to Scala; we assume the reader is familiar with the language. This is a living document that will change to reflect our current “best practices,” but its core ideas are unlikely to change: Always favor readability; write generic code but not at the expensive of clarity; take advantage of simple language features that afford great power but avoid the esoteric ones (especially in the type system). And have fun. Formatting The specifics of code formatting — so long as they are practical — are of little consequence. This is of particular importance to Scala, as its grammar has a high degree of overlap.

We adhere to the Scala style guide plus the following rules. Whitespace Indent by two spaces. Naming Use short names for small scopes Use vals Prefer: Learn. Getting Started Install Scala on your computer and start writing some Scala code! Dive straight into the API. Tutorials Digest bite-size pieces of the essentials. Online Learning There are a few interactive resources for trying out Scala, to get a look and feel of the language: Functional Programming Principles in Scala, free on Coursera. Books There are more and more books being published about Scala. Bleeding Edge If you are interested in finding out about the hottest, most pressing issues of tomorrow in the Scala world, have a look at the Scala Improvement Process (SIP) page. Older Documentation The documentation below may be a bit outdated, but provides insights into the (historical) design principles of the language : Brief Scala Tutorial: a 20 page introduction to scala and some of the basic concepts and a good place to start.