Echoes from 308 logo

Echoes from 308

Subscribe
Archives
May 30, 2021

Scala 3, Pisco Sours, and Twitter

Scala 3

The past week was mainly filled with me pouring over the language reference for Scala 3. It was initially supposed to be released sometime last fall, but as with all software projects, estimation proved to be nearly impossible. For the most part, it looks like a decent improvement in ergonomics over Scala 2. I’ll go over some of my favourite features.

ADTS as Enums

The generally-accepted method of encoding ADTS in Scala was via sealed traits and case classes. For example, I’d encode a binary tree like so:

sealed trait Tree[+A]
case object Leaf extends Tree[Nothing]
final case class Node[+A](data: A, left: Tree[A], right: Tree[A])

Scala 3 introduces enums as the preferred way to go about creating ADTs. The example above is now written as:

enum Tree[+A]:
  case Leaf extends Tree[Nothing]
  case Node[+A](data: A, left: Tree[A], right: Tree[A]) extends Tree[A]

I’m not sure how much typing I save with the new definition, but I would imagine it’s much easier for new developers to understand that ADTs are just fancy enums instead of having to understand what sealed traits, case objects, and case classes all are just to create a simple ADT.

Union and Intersection Types

I was surprised to find out that Scala didn’t have support for union and intersection types. There are a number of hacks on Stack Overflow for getting them to work in Scala 2, but they’re now first-class constructs in Scala 3:

// Type alias for intersection type
type Intersection[A, B] = A & B

// Type alias for union type
type Union[A, B] = A | B

Extension Methods

This is probably the feature that I’m most excited about. It’s now possible to add new properties/values to existing types without needing to use implicits! This means I can now write useful stuff like:

// "opening up" the String type and adding a method
extension (s: String)
  def isRickroll: Boolean = s.contains("dQw4w9WgXcQ")

"https://www.youtube.com/watch?v=dQw4w9WgXcQ".isRickroll // true

I know C# got this feature a while ago, and it’s basically a given thing with dynamically-typed languages like Ruby and Python, but it’s still cool nonetheless.

What I don’t like (so far): Optional Braces

This is a tricky one. The feature is called “Optional Braces” but it might as well have been called “optional significant whitespace.” I understand that this feature is optional, but I feel like it serves to introduce yet another way for the already fragmented Scala community to scatter. Also, given the pretty negative feedback to a PR containing this work, I was rather surprised to see that it made its way into the final version of Scala 3.

Perhaps time will show this to be a killer feature, but for now, I think it’s making the language a little more complex than it needs to be.

Pisco Sours

Yesterday, we had a lab social at Waterfront Park in North Vancouver. It was also my first time riding the Seabus in my 18-19 years living in Vancouver.

Felipe and his partner came with the ingredients needed to make Pisco sours 1, and they were fantastic. I think they’re basically the official drink of the SPL now. I also got the chance to try the “active ingredient” of the beverage (Pisco). It was the first time in a while that I had hard liquor straight. Memories of first and second year came flooding back and I was reminded of why I get sick at the smell of gin.

As we sipped Pisco sours in the sun, we hatched a plan to stock a liquor cabinet in the lab when we’re (hopefully) all back in September. There’s something to look forward to.

Twitter

I start my internship at Twitter this coming Tuesday. I had a call with my “buddy” at Twitter on Friday, and it looks like I’ll be working with some pretty cool people on developing internal tooling that makes data at Twitter “discoverable, usable, and compliant.” I’m pretty excited to be working on internal tooling, as that’s something I’ve always been interested in more so than consumer software.

I’ll hopefully leverage this internship to enrich some of the ideas I have for a thesis.


  1. https://en.wikipedia.org/wiki/Pisco_sour ↩

Don't miss what's next. Subscribe to Echoes from 308:
Powered by Buttondown, the easiest way to start and grow your newsletter.