More Scala

I’ve been doing a ton of reading on Scala and jotting down some observations on what I’ve learned. Some of it is simple but not immediately obvious; some of it is those places where the reading left a desire for more information. Hopefully this will save some time for others trying to pick this up. If not, at least it will be a reference for me.

Some notes from going through Scala School

  • The relationship between partial application and currying is still not totally clear to me; it seems like you curry to create a series of functions then partially apply them in sequence to achieve the full result.
  • The differences between methods and functions are unclear; the tutorial assures me that’s okay but I don’t like it. It seems that functions are strictly things that inherit from function.
  • Type aliases seem like a great way to build new types that imply a collection of existing types.
    • type SocketFactory = SocketAddress => Socket
  • I need a better understanding of objects in scala. Not the normal OO object, the Scala object seems to be closer to the singleton pattern or a static.
  • Guard matching syntax seems interesting, e.g.,
    • case i if i == 1 => “one”
    • I wonder what syntax you can use in that expression. Hopefully function calls are legal.
  • Tuple Accessors are 1 base not 0 based.
  • Destructuring bindings to decompose tuples is awesome, they let you break down tuples quickly and neatly.
    • val (char, digit) = tuple
  • I’m a big fan of GetOrElse on Option, very concise and clear.
  • Partial functions are another idea I haven’t seen before.
    • It is a function that knows it can only produce results for a subset of inputs.
    • They’re the underlying implementation in case statements which allows case statements to be used like functions!
    • Not sure what else I could do with them.
  • I think I’ve figured out F-bounded polymorphism, essentially you define that the generic type must implement the type you are defining. This is so that you can constrain the generic type to pass it into the base class.
  • Structural types look like duck typing but uses reflection so we should avoid using them. 😦
  • Manifests are compiler generated code to counter type erasure. I wonder how much code bloat this causes.
  • Found the DecorateAsJava trait, nice usage of implicit conversions to make interop with java easier.
  • Spies seem like a nice feature of the testing framework. Seems like you could build inconsistent behavior in an object like a list that says it has some number of objects but doesn’t have an entry at that point.
  • The section on traits did not make the mixin functionality clear. You can mixin a trait as part of the constructor of an object not just as part of the class declaration! That’s not quite C# extension methods but close enough for some usages.
    • Found implicit classes elsewhere that can be used almost exactly like extension methods.

Notes from Scala Exercises

  • The operator, ie fat arrow =>, shows as one character in some of the examples which is confusing.
    • Similar problems with the skinny arrow ->
  • Backticks in pattern matching allow matching against other variables.
    • def patternEquals(i: Int, j: Int) = j match {
       case `i` ⇒ true
       case _ ⇒ false
  • There seems to be usages of implicits that work like extension methods, but you need more syntax.
    • class KoanIntWrapper(val original: Int) {
       def isOdd = original % 2 != 0

      implicit def thisMethodNameIsIrrelevant(value: Int) = new KoanIntWrapper(value)
  • The section on for expressions did not really help my understanding.
    • val xValues = 1 to 4
      val yValues = 1 to 2
      val coordinates = for {
       x ← xValues
       y ← yValues
      } yield (x, y)
    • In this example it isn’t clear why the two lists are evaluated in any particular order.
  • Scala Fiddle was a great help for running some of the examples and being able to try some variations.
  • I’m not sure how I feel about the conversion between methods and operators where certain kinds of methods can be used as prefix/infix/unary operators. Having seen a C++ codebase with off operator implementations I realize the pain it can cause; maybe since you can name them however you want they’ll read better.

I ended up going through the Play Framework Tutorial as well since it’s part of the stack I’ll be using.

  • Looks a lot like ASP.Net MVC.
  • Session data signed to prevent tampering.
  • @With annotation and allowing composition of actions.
  • Twirl temples look a lot like razor templates except that you add dynamic elements in Scala not C#.
  • Play seems like it just composites together best practices from various other JVM libraries bringing together JPA, Twirl, Jackson, Akka, Guice, and logback.
  • The WithApplication class seems like a way to make building testable controllers easier.
  • Similarly with WithServer class will help test some fully remote calls.

Overall I’m feeling pretty psyched. At this point I feel pretty comfortable reading the sample code. The exercises were more about reading it and understanding it rather than writing your own code.

Since the language is so big I’m not sure which parts are where I should be focusing. I feel like I need to start building something with it at this point to understand how to decompose a class into traits. I’m not sure the Project Euler problems will supply the appropriate domain to try and build these more complex inheritance hierarchies. I’m also not certain if I’ll be building good Scala code or just mashing together different styles in a way that wouldn’t scale. Maybe Scalastyle can help with that, but the rules don’t seem to be the kind of thing I’m looking for.

I’m going to forge ahead with this list of Scala problems since I hope that being able to contrast my solutions with the provided ones will give me some level of feedback on what I’m doing. Then probably move into the Project Euler problems afterwards.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s