I am the founder and lead developer of Hibernate Envers, a Hibernate core module, which provides entity versioning/auditing capabilities. I am also one of the co-founders of SoftwareMill, a company specializing in delivering customized software solutions (http://softwaremill.com, "Extraordinary software as a standard"), based on Java and JBoss technologies. After work, apart from being involved in development of Envers, I work on several small open source projects, like ElasticMQ (simple message queue written in Scala with an SQS interface), projects around static analysis (using JSR 308 - Typestate Annotations/ Checkers Framework and FindBugs), and some CDI/Weld (not always portable) extensions, like autofactories or stackable security interceptors. I am also interested in new JVM-based languages, especially with functional elements (like Scala, JRuby) and frameworks built using them (like Lift), as well as improving the ways we use Dependency Injection. Adam is a DZone MVB and is not an employee of DZone and has posted 52 posts at DZone. You can read more from them at their website. View Full User Profile

Missing OO and FP bridge in Scala

  • submit to reddit

Scala blends functional and object-oriented programming in many nice ways. You can use both FP an OO-like constructs whichever fits the current problem better. But there’s always room for improvement! Here’s one thing I think is missing (short version at the bottom).

FP side

It’s easy to convert a method to a function in Scala. For example, if you have a method:

def say(to: Person, what: String): String = { ... } 

we can get the corresponding function using the underscore notation:

val sayFun: (Person, String) => String = say _

Moreover, Scala supports multiple parameter lists, which is sometimes also referred to as currying, and which makes partial application easier:

// Method with multiple parameter lists
def connect(person1: Person)(person2: person): Connection = { ... }
// Function, created by partially applying the previous method
val createConnectionWithAdam: Person => Connection =
      connect(new Person("Adam")) _

OO side

One thing every class has is a constructor. But what is a constructor, really? You give values for the constructor’s arguments, and get a new instance of the class in return. So it’s really just a function!

class Person(name: String, age: Int) { ... }
// The "signature" of new is (String, Int) => Person
val somebody = new Person("John", 34)


However, that’s where the combination of OO and FP fails in Scala: you can’t use the two features of methods (convert to function, currying) mentioned above with constructors. Both of these won’t work:

val makePerson: (String, Int) => Person = new Person _
class Person2(name: String)(age: Int) { ... }
val makeNewJack: Int => Person = new Person2("Jack") _

You can get around this using companion objects and apply (or any other factory method, apply just has a nicer notation afterwards):

object Person2 {
   def apply(name: String)(age: Int) = new Person2(name, age)
val makeNewJack: Int => Person = Person2("Jack") _

But that requires repeating the signature of the constructor in the companion object, and nobody likes code duplication, right? ;)


Where can this be useful? For example in the classic factory example. Imagine you have a class which depends on some services, but also on some data available on runtime. Of course we use IoC so instances of the other services are provided to our class:

// This service depends on a concrete Person instance
class Service3(service1: Service1, service2: Service2)(person: Person) {
// Note that the multiple parameter notation above als provides a nice
// separation of the parameters that should be "injected" - services,
// and the data that can be variable.
// This service depends on Service1, and wants to create it having Person
// instances
class Service4(makeService3: Person => Service3) {
  // Usage:
  for (person <- persons) makeService3(person).doSomething()
class Main {
   // Bootstrap: (or - no-framework DI container ;) )
   val service1 = new Service1
   val service2 = new Service2
   // That's the part that is illegal in Scala
   val makeService3 = new Service3(service1, service2) _
   val service4 = new Service4(makeService1)
   // Today we'd have to write: val makeService3 =
   //     (person: Person) => new Service3(service1, service2, person)

That’s also connected to my post on DI and OO, and how current DI frameworks make it hard to define services which depend on data and services that we’d like to have multiple copies of.

Side note

When viewing constructors as methods/functions (which they are, really ;) ), I suppose the Ruby-like notation:

Person.new("John", 34)

would be better and adding support for _ and multiple parameter lists would be obvious.

Bottom line for TL;DR fans

Why not treat class constructors as every other method/function? Make this legal:

class Person(name: String, age: Int)
val makePerson = new Person _ // type: (String, Int) => Person
class Person2(name: String)(age: Int)
val makeNewJack = new Person2("Jack") _ // type: Int => Person


Published at DZone with permission of Adam Warski, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)


Silvio Bierman replied on Mon, 2012/09/03 - 8:02am

May be off topic here but anyway... 


val makeJack = i => new Person2("Jack")(i)

or even

val makeJack = new Person2("Jack")(_) 


Both of which are equal. Underscore syntax sugar is a shorthand form that does not always work where the full form will. 

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.