Mark Needham

Thoughts on Software Development

Scala: Setting default argument for function parameter

without comments

Yesterday I wrote about a problem we’ve been having with trying to work out how to default a function parameter that we have in one of our methods.

Our current version of the code defines the function parameter as implicit which means that if it isn’t passed in it defaults to Predef.conforms():

def foo[T](bar: String)(implicit blah:(String => T)) = { 
  println(blah(bar)); 
  bar 
}

It’s not entirely clear just from reading the code where the implicit value is coming from so we want to try and make the code a bit more expressive.

The way we wanted to do this was by making ‘blah’ have a default value rather than making it implicit.

Our equivalent to Predef.conforms() is the identity function and our first attempt at defaulting the parameter looked like this:

def foo[T](bar: String, blah:(String => T) = identity _) = { 
  println(blah(bar)); 
  bar 
}

Unfortunately when we try to use that function without providing the second argument we get the following exception:

scala> foo("mark")
<console>:18: error: polymorphic expression cannot be instantiated to expected type;
 found   : [T](Nothing) => Nothing
 required: (String) => ?
Error occurred in an application involving default arguments.
       foo("mark")

From what I understand the compiler is unable to infer the type of the input parameter, a problem we can fix by explicitly specifying that:

def foo[T](bar: String, blah:(String => T) = identity[String] _) = { println(blah(bar)); bar }

We can then either choose to provide a function:

scala> foo("mark", _ + "needham")
markneedham
res17: String = mark

…or not:

scala> foo("mark")
mark
res16: String = mark

This solves the problem for this simple example but an interesting problem that we then ran into is that we actually had overloaded versions of the method in question and only one overload is allowed to specify default arguments as per the spec.

Each overload actually takes in different parameter types so one way to get around this problem would be to make some of the parameters optional and then default them to None.

At the moment we’ve ended up leaving the implicit conversion in because the change is a bit bigger in nature than antiticpated.

Be Sociable, Share!

Written by Mark Needham

November 8th, 2011 at 10:46 pm

Posted in Scala

Tagged with