All Articles

A Tiny Syntactic Sugar for Wrapping a Scala Class with Serializable Trait

sweet alice

In Scala / Spark programming, we sometimes need to implement the Serializable trait for a class, for passing the data between the Spark executors. To achieve this, we could declare a companion object with an apply method, and then use the apply method to create an instance of the class. Here’s a tiny syntactic sugar for this.

import scala.reflect.ClassTag

class SerializeVariable[T: ClassTag](constructor: => T) extends AnyRef with Serializable {
  @transient private lazy val instance: T = constructor
  def get: T = instance
}

object SerializeVariable {
  def apply[T: ClassTag](constructor: => T): SerializeVariable[T] = new SerializeVariable[T](constructor)
}

The usage is simple, just wrap the class with the SerializeVariable class, and then use the get method to get the instance.

// wrapper
val normalizer = SerializeVariable(new JsonRegexAspectNormalizer())
// extract
def extract(normalizer: SerializeVariable[JsonRegexAspectNormalizer]): JsonRegexAspectNormalizer = normalizer.get

That’s all. It’s a simple solution and easy to use. I hope you like it.

Published Jul 4, 2019

Flying code monkey