Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Scala: Setting default argument for function parameter

DZone's Guide to

Scala: Setting default argument for function parameter

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

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.

 

From http://www.markhneedham.com/blog/2011/11/08/scala-setting-default-argument-for-function-parameter

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}