Friday, September 4, 2015

Scala for the C# Web Developer: Part 1 - Overview

I'm currently accepting new clients and projects! If you need an experienced Software Developer, please contact me.

Why bother?

While I've always been quite happy with the .NET ecosystem in general, there's always been a tiny voice in the back of my mind that wasn't comfortable letting Microsoft rule the destiny of my software projects.

Not that I don't trust them, but businesses need to make business decisions, and Microsoft's licensing decisions haven't always aligned with the small time/hobby developer. This isn't to say that they haven't changed immensely over the past few years (they have), or that they won't continue to make more open decisions in the future (they will).

I'm not here to persuade you into changing platforms, simply offering an alternative to those who are looking for one like I was. Personally, I just like the idea of being free from both costs and restrictions with technologies that I can use today. And if there's something out there that can make me just as productive, but it's free and open source, why not? I decided I'd never know unless I try.

Some of the nagging thoughts/issues I've had in the past include:

  • What if my SQL Database outgrows 10 GB?
  • Can I set up a test environment without worrying about licensing?
  • If I need to spin up another instance, do I need more licenses?
  • Wow, does a Windows install really require over 20 GB?
  • How in the world can I automate [X]?

A lot of this has changed now that the cloud/Azure takes care of the licensing, provisioning and storage issues for you, but I still prefer the flexibility of being able to deploy just about anywhere.

Why C#/.NET?

There are a lot of good reasons that I stayed on the .NET stack for this long:

  • IDE - Visual Studio is by far the best development experience on the market (IMO, of course). Even better now that it's free.
  • Language - I like languages with a lot of compile time guarantees. The quicker I can find errors, the better. I also like the functional features (lambdas specifically) that come with C#.
  • Toolbelt - I've been a .NET developer for so long that all the major hurdles (deployment, artifact repository, tools, quirks, etc) are a known quantity.
  • Familiarity - I know the style, commonly used patterns, and the most popular libraries to get things done.

I'm a web developer, so specifically this means I was looking to replace these technologies:

  • An OO/functional language with static type guarantees
  • Visual Studio
  • Entity Framework
  • SQL Server
  • SQL Database Project (.dbproj)
  • NuGet

Keeping these values in mind, I searched for a suitable open source full stack replacement.

Why Scala?

As I quickly found out, there aren't a ton of statically typed, managed languages that mix functional and OO paradigms with relatively large ecosystems that are ready for production. Scala felt the most familiar out of everything I looked at, and there are a lot of big name companies that have deployed it in production. I'm more about trying something than spending time reading up on every facet of the language up front, so I decided to give it a shot and let it succeed or fail based on my experience.

At this point, I'm guessing you've probably at least heard of Scala, but you may not know much about it. Let's start with some comparisons to get you familiar with the techniques you're already using.

Note that I'll be writing these examples with a C# style to start out with, and then show you the common Scala style at the end.

Basic C# to Scala Examples

Scala is a statically typed language that runs on the JVM. On first glance, it kind of looks like a cross between Ruby and C#:

Note that the biggest difference is that the default constructor is declared with the class name. If you want to do any work during the construction of this class, you'd do that directly in the class body (similar to Javascript):


It supports class inheritance, just like C#. Note that the call to base is actually set directly on the base class declaration, since this is the default constructor:


You can declare an interface by using traits:

Static Classes

One interesting feature of Scala is that there are no static classes. Instead, you would use an object, which is instantiated automatically at runtime as a singleton:

Objects can be accessed from anywhere by calling them by name (similar to a static class, you do not need to instantiate it):


You can also create properties with getters and setters:

Note that we don't actually need parenthesis on method declarations. This is up to you to determine each time you create a new member. You can think of omitting the parenthesis as if you are creating a getter property, and adding parenthesis as if it were a normal method. Very similar to C#.


No Return Keyword

Another important thing to note is that everything evaluates to a value in Scala. This basically means that everything has to return something - there is no void (returning a Unit is the equivalent of void). Since each method you write has to return something, it's redundant to write the "return" keyword at the bottom of each method.

No Semicolons

Like Ruby, Javascript and a myriad of other languages, Scala has semicolons but they are rarely used.

Omitting brackets

Since everything evaluates to a value, you actually don't need brackets for one-liner functions.

String Interpolation

Similar to Ruby and C# 6.0, Scala has a really nice string interpolation feature:

Updating our Style

Tying this all together, we can update the Person example to match the conventional Scala style:

Of course there's tons more to learn, but we'll get to that as we move onto Part 2: More Scala Concepts. Thanks for reading!

I'm currently accepting new clients and projects! If you need an experienced Software Developer, please contact me.


  1. I've only used intilliJ when writing scala. Can you use Visual Studio? If so how do I set it up?

  2. Not that I know of, hopefully this will come in the near future since MS seems to be embracing other languages more readily these days. I also use IntelliJ, it seems to be the next best thing to Visual Studio/ReSharper. Certainly not as smooth, but I've found that I can be quite productive with it now that I have it set up similarly to VS.

  3. The Scala team should hire you to redo their documentation for them. I've learned more from this article than visiting their site.