scala

Tutorials about the Scala programming language.

Scala, isInstanceOf, inheritance, getClass, and equals (equality)

I was doing a little Scala programming this morning, and because I hadn't written any code in a while, I managed to forget how isInstanceOf works with inheritance in Scala.

To refresh my memory, I wrote the following example code:

Adding JSON to the body of an HTTP POST request in Scala or Java

If you ever need to write some Scala (or Java) code where you add a JSON string to the body of an HTTP POST request, here's a quick example of how to do it using the Apache HttpClient library methods:

Making a web service request with a timeout from a Play Framework Controller

My original "Day 1 with Play web services" code is shown a few paragraphs below, but on Day 2, the following code looks like an easier solution to the problem, courtesy of this web page:

A shell script to build a Scala SBT project directory structure

Just a quick note here that I'm developing a simple shell script to build a Scala SBT project directory structure. Here's the shell script in its current incarnation:

Scala SBT and build.sbt syntax and examples

Note: This page is very much a work in progress.

A sample Scala SBT build.sbt file, including the last line, which handles the SBT 're-run with -deprecation for details' warning message. (If you get the 're-run with -deprecation' message, that last line hands the "-deprecation" option over to the compiler, so you can see the deprecation problems.)

Scala REPL "She's gone rogue" error message

In a little bit of accidental Scala REPL fun this afternoon, I was testing a recursive algorithm like the following simple factorial algorithm, and forgot a return statement:

def factorial(n: Int):Int = {
  if (n == 1) n    // forgot 'return' here
  factorial(n - 1)
}

When I pasted that code into the Scala REPL and then tried to test it as shown below, it didn't come back for a while, until it failed with this error message:

The Factory Pattern in Scala

While working on a new Scala application recently, the alarms started going off in my head, saying "You need to use the Factory Pattern here." That's when things got interesting.

Just exactly how do you implement the Factory Pattern in Scala?

Here's a simple, quick example.

(1) The interface

Sparing you all the experiments I went through, let's assume you want a factory that produces animals like this:

Using GitHub projects as Scala library dependencies with sbt and sbteclipse

Okay, this is pretty cool. With sbt, you can magically refer to dependencies that are set up as GitHub projects, and those projects don't need to have a jar file, they can just be a source code library. This means you can save your Scala libraries as source code to GitHub (like you normally would), then pull them into your other Scala projects simply by referencing them in your build.sbt file.

Assuming you're comfortable with sbt, here's a quick six-step example that shows how to pull a GitHub library into a simple Scala project:

Making Twitter web service calls concurrently with Akka Futures

While I was working on improving how Sarah gets information from Twitter and other sources, I decided to take some of that code and hack together this example. It shows how to make three Twitter web service requests concurrently -- and then wait for ther results before moving on -- with Akka Futures.

Before sharing the entire class, the cool Akka Futures stuff is in these lines of code:

A Scala implicit method argument and field example

UPDATE: This example shows how to create an implicit method in Scala 2.9 or older. You can use a slightly simpler approach with Scala 2.10 and newer, which I've documented in this Scala 2.10 implicit class example.

I'm not going to do much writing here today, but instead I'll just demonstrate how an implicit method argument works with implicit fields in Scala. Without any further ado, here's some code:

Syndicate content