Thursday, December 16, 2010

Graham's Guide to Learning Scala

It's a pretty widely-accepted view that, as a programmer, learning new languages is a Good Idea (tm). Most people with more than one language under their belt would say that learning new languages broadens your mind in ways that will positively affect the way you work, even if you never use that language again.

With the Christmas holidays coming up and many people likely to take some time off work, this end of the year presents a great opportunity to take some time out from your week-to-week programming grind and do some learning.

With that in mind, I present "Graham's Guide to Learning Scala". There are many, many resources on the web for learning about Scala. In fact, I think there's probably too many! It would be quite easy to start in the wrong place and quickly get discouraged.

So this is not yet another resource to add to the pile. Rather, this is a guided course through what I believe are some of the best resources for learning Scala, and in an order that I think will help a complete newbie pick it up quickly but without feeling overwhelmed.

And, best of all, it has 9 Steps!

[Note: Many of the resources have a slant towards teaching programmers that know Java, but I imagine if you know any other popular OO language like C++, C#, Ruby, Python, Objective-C or Smalltalk, you shouldn't have a problem picking it up.]

Are you ready? Here goes...

Step 1: Lay the Foundation

Read 'Introduction to the Scala Language' on scala-lang.org

This single page is a succinct but dense description of Scala and will let you know what you're in for, and why you should keep going.


Step 2: Get the Tools

Download and install the latest Scala release

Download and install the latest IntelliJ IDEA *Community Edition* release

Open IDEA and install the 'Scala' plugin


Step 3: Hello, World!

Create a new IDEA project with a Scala module.
Create a new file called HelloWorld.scala and paste in the following code:

object HelloWorld {
def main(args: Array[String]) {
println("Hello, world!")
}
}

Hit Ctrl-Shift-F10 to run it.
Congratulations! First Scala program done and dusted!


Step 4: Commute your Java skills to Scala

Read Daniel Spiewak's 'Scala for Java Refugees' series.

This should give you enough knowledge and experience with Scala syntax to be able to write Scala that does most of the things you can do in Java.

Make sure you enter the code examples into IDEA and try them out as you go. I suggest typing them out instead of copy-paste, because that's likely to give you exposure to common syntax mistakes and compiler errors.


Step 5: Open Your Eyes

Read through the 'Tour of Scala' on scala-lang.org

These short pages will give you some exposure to some of the more advanced features of Scala. Don't worry if you don't understand everything - just keep reading. You're not trying to learn how to use all these things, just to know that they're around.


Step 6: Learn the Basics, the Ins & the Outs

Read chapters 1 to 8 of Dean Wampler & Alex Payne's 'Programming Scala' (O'Reilly)

This is where the learning gets heavy. We're talking about more than half a book here. You'll learn everything from basic syntax (some of which you'll already know from the Refugees series) to Scala's more advanced patterns for object-oriented programming, an introduction to Scala's functional programming capabilities and some of the curly edges of Scala's type system.

Again, make sure you try out running the code samples in IDEA. I suggest you also have a play around with some of the concepts in your own little test classes. Any time you find yourself thinking "I wonder if I can use this feature to do this?", grab a keyboard and find out for yourself.


Step 7: Open the Dragon Scroll and Release your Scala Fu

Take a break from learning and do some hacking!

Think of a simple idea that you'd like to implement in Scala, then code it! (Seriously, I recommend keeping it REALLY simple.) If you can't think of something yourself, you can steal my idea of writing a function to perform word-wrapping.


Step 8: Take a Walk around the Grounds

Read the rest of 'Programming Scala' (chapters 9 to 14)

The rest of the book covers some of the peripheral libraries and concepts in Scala that contribute to making it a great language, like Agents for multi-threaded programming, "herding" XML input and output and building blocks for creating domain-specific languages, as well as reviewing some of the tools that have sprung up in the Scala ecosystem.


Step 9: Go Forth and Hack!

This is where the directed part of the journey ends, but hopefully it's only the first step.

What you do with Scala next is up to you, but by this point you know more than enough about Scala to start using it seriously at home or even at work, for fun, or for making some serious cash! If you do make some serious cash by hacking Scala, please remember how it all started and send a little monetary "Thank you" my way. ; )


Addendum: Getting Help

Perhaps it was a little presumptuous of me to effectively say "go away now and code". Chances are, you'll need some help as you keep experimenting, learning and doing more and more cool stuff with Scala.

The two best places that I've found to connect with other Scala users are the scala-user mailing list run by scala-lang.org and, for curly technical problems that you just can't figure out yourself, stackoverflow.com. Actually, reading through other people's Scala questions on Stack Overflow can also be a great way to pick up new ideas!


Want to learn more?

From Amazon...

From Book Depository...

10 comments:

  1. Graham, from my own experience I found Scala's REPL to be useful to get started. What do you think about that?

    ReplyDelete
  2. Hi firstname (!?)

    Thanks for your comment.

    Yep, the REPL (Read-Evaluate-Print Loop) is good for some things. It's covered in the first chapter of the O'Reilly book. Personally, I've found that it's pretty neat for trying out little tests for one or two lines of code, but anything more than that and it starts to get a bit unwieldy.

    I think the main disadvantage is that if you write a multi-line function and then decide you need to edit it, the history only shows one line at a time, so you have to re-enter each line of the function - painful.

    It has it's place, but I reckon that getting a fully-functional IDE set up ASAP is probably the better way to start experimenting.

    Cheers,

    Graham.

    ReplyDelete
  3. Hi Graham, thank you for the cool guide and tips. I've forwarded the link to my co-workers.
    What do you think about using Netbeans and/or Eclipse for Scala development? Also, I'd appreciate your comments about the general usage growth of Scala. Why do you think it's not growing faster?

    ReplyDelete
  4. Someone on the Scala mailing list has also recommended this "Scala Cheat Sheet":
    http://anyall.org/scalacheat/

    It's not the kind of thing that you want to try and learn from, but once you've got some of the basics under your belt it would probably make a good reference.

    ReplyDelete
  5. Don't forget ensime - not mainstream but the fastest of the lot ;)

    ReplyDelete
  6. Hi Marcos,

    I can't comment personally on Eclipse or NetBeans because I haven't used either of them for Scala development. I have, however, seen quite a few anecdotal comments about the Eclipse plugin being unstable. You can read more in these two discussions on Stack Overflow: this older one and this newer one (specifically about 2.8 support). There was also a recent discussion on the scala-user mailing list about the best Scala IDE.

    In regards to the growth of Scala, I think that's an interesting question. While I've got some thoughts on the matter, I thought it would be better answered by collecting a range of responses, so I've created a question on StackOverflow to try and get exactly that. You can see my opinions there as answers to the question.

    Cheers,

    Graham.

    ReplyDelete
  7. Any ideas on best way to learn the scala libraries?

    The challenge I find is not the language, nor functional style, but understanding when there is a Scala library (and idiom for using it), and when there isn't and you just fall back to the Java libraries.

    This seems the big challenge in languages today. The libraries are huge. In the case of scala libraries, some of them are little DSLs, and so are very much idiom-creating.

    ReplyDelete
  8. Hi Mike.

    I can't give too much opinion on libraries. A lot of my work has been in getting comfortable with using Java libraries in Scala code, e.g. Spring, Hibernate, JUnit and WebDriver/Selenium. There is some new learning to do here, because you can use them in much more efficient ways in Scala than is possible in Java, partly due to the possibilities of Pimp My Library

    If you would like some lists of Scala libraries that you can investigate, there is a question containing several such lists on SO as well as another one about web frameworks.

    In regards to choosing which ones to look into, I often judge a library by it's documentation: if the author hasn't gone to the effort of writing good API docs and AT LEAST a decent 'Quickstart Guide', I usually assume that they're not serious enough about their library and have probably either done a good job at designing it or will not put any effort into maintaining and improving it.

    For what it's worth, the one Scala library that I have used quite a bit is the Specs testing library for writing BDD-style tests. I'm not a huge fan of BDD, but for some of the components I've written it has come quite in handy. (And it meets my criteria above about documentation.)

    Cheers,

    Graham.

    ReplyDelete
  9. PS: That should have read, "..have probably either NOT done a good job at designing it..."

    I did not do a good job of reviewing that comment!

    ReplyDelete