Principal Data Scientist, Practice Lead for Data Science, AI, Big Data Technologies at Teradata. O’Reilly author on distributed computing and machine learning. ​

Natalino leads the definition, design and implementation of data-driven financial and telecom applications. He has previously served as Enterprise Data Architect at ING in the Netherlands, focusing on fraud prevention/detection, SoC, cybersecurity, customer experience, and core banking processes.

​Prior to that, he had worked as senior researcher at Philips Research Laboratories in the Netherlands, on the topics of system-on-a-chip architectures, distributed computing and compilers. All-round Technology Manager, Product Developer, and Innovator with 15+ years track record in research, development and management of distributed architectures, scalable services and data-driven applications.

Saturday, July 14, 2012

Scala class/object companion pattern

A Scala companion object is an object with the same name as a it own class class, hence the "companion" name. The object has access to all of that class’ members, including the private ones.

Typical usage:

class Apple private (val color:String) {
// Stuff goes here...

object Apple {
def apply(color:String):Apple = new Apple(color)

val greenApple = Apple("blue")
val redApple = Apple("white")

One of the main reason to use the companion pattern is to overload the constructors and redefine the behavior of the apply method. This turns very useful when you want to define a factory or when you want to treat an object as a primitive type.

Furthermore, objects serve the same (and some additional) purposes as the static methods and fields in Java.