Programming in Scala: A Comprehensive Step-by-step Guide

Category: Programming
Author: Martin Odersky, Lex Spoon, Bill Venners
4.4
This Year Stack Overflow 1
This Month Stack Overflow 2

Comments

by anonymous   2017-08-20

Edit4:

Moved the original (2009) code I had here to a gist.

Recently (August 2012) I wrote a series of blog posts on writing tetrix in Scala. I hope it answers some of the questions I asked three years ago.

  • tetrix in Scala

Edit: Updated the code to use Scala's Swing API as suggested by @thatismatt. Also, I've found that the way to learn Scala style coding is to read the book by Martin Odersky called Programming in Scala. In essence, the Scala way is gradually shift from imperative style to functional style by using immutable data structure, functions without side-effects, pattern matching, traits, etc.

Edit2: Updated the code to use immutable data structure etc. For example Block#rotate now returns a new Block object instead of modifying itself.

Edit3: Updated the code to work on Scala 2.8 RC3.

by anonymous   2017-08-20

Aleksander's answer is correct, but Programming in Scala offers an additional alternative:

sealed trait Foo {
 // interface
}

object Foo {
  def apply(...): Foo = // public constructor

  private class FooImpl(...) extends Foo { ... } // real class
}