41

I'm working on a Scala API (for Twilio, by the way) where operations have a pretty large amount of parameters and many of these have sensible default values. To reduce typing and increase usability, I've decided to use case classes with named and default arguments. For instance for the TwiML Gather verb:

case class Gather(finishOnKey: Char = '#', 
                  numDigits: Int = Integer.MAX_VALUE, // Infinite
                  callbackUrl: Option[String] = None, 
                  timeout: Int = 5
                  ) extends Verb

The parameter of interest here is callbackUrl. It is the only parameter which is really optional in the sense that if no value is supplied, no value will be applied (which is perfectly legal).

I've declared it as an option in order to do the monadic map routine with it on the implementation side of the API, but this puts some extra burden on the API user:

Gather(numDigits = 4, callbackUrl = Some("http://xxx"))
// Should have been
Gather(numDigits = 4, callbackUrl = "http://xxx")

// Without the optional url, both cases are similar
Gather(numDigits = 4)

As far as I can make out, there are two options (no pun intended) to resolve this. Either make the API client import an implicit conversion into scope:

implicit def string2Option(s: String) : Option[String] = Some(s)

Or I can redeclare the case class with a null default and convert it to an option on the implementation side:

case class Gather(finishOnKey: Char = '#', 
                  numDigits: Int = Integer.MAX_VALUE, 
                  callbackUrl: String = null, 
                  timeout: Int = 5
                  ) extends Verb

My questions are as follows:

  1. Are there any more elegant ways to solve my particular case?
  2. More generally: Named arguments is a new language feature (2.8). Could it turn out that Options and named default arguments are like oil and water? :)
  3. Might using a null default value be the best choice in this case?
DaGGeRRz
  • 1,611
  • 1
  • 12
  • 13
  • Thanks for a lot of good answers for question 1 and 3! I'll implement some more sample uses cases of API usage before deciding whether to use Aaron's Opt, stick with Option or just use a null default. Too bad there are no obvious choices, though - maybe we need a new language feature to make the choice a no-brainer? – DaGGeRRz Nov 17 '10 at 15:53

7 Answers7

25

Here's another solution, partly inspired by Chris' answer. It also involves a wrapper, but the wrapper is transparent, you only have to define it once, and the user of the API doesn't need to import any conversions:

class Opt[T] private (val option: Option[T])
object Opt {
   implicit def any2opt[T](t: T): Opt[T] = new Opt(Option(t)) // NOT Some(t)
   implicit def option2opt[T](o: Option[T]): Opt[T] = new Opt(o)
   implicit def opt2option[T](o: Opt[T]): Option[T] = o.option
}

case class Gather(finishOnKey: Char = '#', 
                  numDigits: Opt[Int] = None, // Infinite
                  callbackUrl: Opt[String] = None, 
                  timeout: Int = 5
                 ) extends Verb

// this works with no import
Gather(numDigits = 4, callbackUrl = "http://xxx")
// this works too
Gather(numDigits = 4, callbackUrl = Some("http://xxx"))
// you can even safely pass the return value of an unsafe Java method
Gather(callbackUrl = maybeNullString())

To address the larger design issue, I don't think that the interaction between Options and named default parameters is as much oil-and-water as it might seem at first glance. There's a definite distinction between an optional field and one with a default value. An optional field (i.e. one of type Option[T]) might never have a value. A field with a default value, on the other hand, simply does not require its value to be supplied as an argument to the constructor. These two notions are thus orthogonal, and it's no surprise that a field may be optional and have a default value.

That said, I think a reasonable argument can be made for using Opt rather than Option for such fields, beyond just saving the client some typing. Doing so makes the API more flexible, in the sense that you can replace a T argument with an Opt[T] argument (or vice-versa) without breaking callers of the constructor[1].

As for using a null default value for a public field, I think this is a bad idea. "You" may know that you expect a null, but clients that access the field may not. Even if the field is private, using a null is asking for trouble down the road when other developers have to maintain your code. All the usual arguments about null values come into play here -- I don't think this use case is any special exception.

[1] Provided that you remove the option2opt conversion so that callers must pass a T whenever an Opt[T] is required.

Community
  • 1
  • 1
Aaron Novstrup
  • 20,967
  • 7
  • 70
  • 108
  • I used to use `null` defaults, but I really like this solution. – Alexey Romanov Nov 17 '10 at 08:30
  • Nice. It solves all problems, but has the downside of introducing a new, unknown (to the user) type in the API interface. – DaGGeRRz Nov 17 '10 at 15:46
  • @DaGGeRRz True, but I'd argue that the cost is negligible. It's a utility type that users only need to learn once to benefit from throughout your codebase, and a very simple type at that. – Aaron Novstrup Nov 17 '10 at 19:56
9

Don't auto-convert anything to an Option. Using my answer here, I think you can do this nicely but in a typesafe way.

sealed trait NumDigits { /* behaviour interface */ }
sealed trait FallbackUrl { /* behaviour interface */ }
case object NoNumDigits extends NumDigits { /* behaviour impl */ }
case object NofallbackUrl extends FallbackUrl { /* behaviour impl */ }

implicit def int2numd(i : Int) = new NumDigits { /* behaviour impl */ }
implicit def str2fallback(s : String) = new FallbackUrl { /* behaviour impl */ }

class Gather(finishOnKey: Char = '#', 
              numDigits: NumDigits = NoNumDigits, // Infinite
              fallbackUrl: FallbackUrl = NoFallbackUrl, 
              timeout: Int = 5

Then you can call it as you wanted to - obviously adding your behaviour methods to FallbackUrl and NumDigits as appropriate. The main negative here is that it is a ton of boilerplate

Gather(numDigits = 4, fallbackUrl = "http://wibble.org")
Community
  • 1
  • 1
oxbow_lakes
  • 133,303
  • 56
  • 317
  • 449
  • If the solution is importing an implicit conversion, I see that this is less likely to make a mess than importing string2option, but I'd like to avoid conversions altogether. On the API implementation side, it would be less effort to just handle the nulls... – DaGGeRRz Nov 16 '10 at 22:21
  • 2
    To clarify on my other comment. Your approach is nice in the sense that it's type safe and would be very powerful if i needed more behavior in the passed arguments. But I don't. :) Adding a wrapper class and implicit conversion for all argument types is IMO too clunky. Also, it makes the API docs much less clear. The signatures will indicate WrapperX type params when just ints and strings could be used... – DaGGeRRz Nov 16 '10 at 22:46
  • 1
    Yes - I totally agree with all of your points. Personally I would go with your original suggestion; i.e. default to `Some("url")` – oxbow_lakes Nov 16 '10 at 22:52
  • We're all in the No Nulls doctrine, I guess. Come to think of it, the URL would typically be built with some sort of URL builder (AbsoluteUrl.apply(relativeUrl: String)) which could return an option, so I might have gotten a lucky break. :) Question 2 still remains, though. – DaGGeRRz Nov 16 '10 at 23:02
5

Personally, I think using 'null' as default value is perfectly OK here. Using Option instead of null is when you want to convey to your clients that something may not be defined. So a return value may be declared Option[...], or a method arguments for abstract methods. This saves the client from reading documentation or, more likely, get NPEs because of not realizing something may be null.

In your case, you are aware that a null may be there. And if you like Option's methods just do val optionalFallbackUrl = Option(fallbackUrl) at the start of the method.

However, this approach only works for types of AnyRef. If you want to use the same technique for any kind of argument (without resulting to Integer.MAX_VALUE as replacement for null), then I guess you should go with one of the other answers

IttayD
  • 28,271
  • 28
  • 124
  • 178
  • I would go with that solution - because the only reason you are using an `Option` is to get a state where there is no value. But such a state already exists in `null`, and you would have to handle it anyway. – Jean Hominal Nov 17 '10 at 10:09
  • Yeah, I'm leaning towards using null and you argue well for it. Regarding Integer.MAX_VALUE, it's used to indicate that the default number of digits are infinite. – DaGGeRRz Nov 17 '10 at 14:42
  • 2
    Actually, I would argue strongly against using a `null` default value for a case class field. Since the field is automatically public, all the usual reasons for avoiding `null`s are applicable. Even within the class definition, having that potential `null` value is error-prone when you factor in long-term maintainability. – Aaron Novstrup Nov 18 '10 at 01:21
  • In that case, make it a private '_fallbackUrl' and use 'val fallbackUrl = Option(_fallbackUrl)'. you get a nice Option val while still maintaining ease of use for the client of the class – IttayD Nov 18 '10 at 04:14
  • 1
    Ease of use suffers when some of the constructor arguments have leading underscores and others do not: `Gather(timeout = 10, _callbackUrl = "http://my.org")`. While the _field_ is private, its _name_ is still part of the public API. Furthermore, the fact the the public field (`callbackUrl`) has a different name than the constructor argument (`_callbackUrl`) is likely to lead to confusion. – Aaron Novstrup Nov 18 '10 at 21:08
4

I think as long as no language support in Scala for a real kind of void (explanation below) ‘type’, using Option is probably the cleaner solution in the long run. Maybe even for all default parameters.

The problem is, that people who use your API know that some of your arguments are defaulted might as well handle them as optional. So, they’re declaring them as

var url: Option[String] = None

It’s all nice and clean and they can just wait and see if they ever get any information to fill this Option.

When finally calling your API with a defaulted argument, they’ll face a problem.

// Your API
case class Gather(url: String) { def this() = { ... } ... }

// Their code
val gather = url match {
  case Some(u) => Gather(u)
  case _ => Gather()
}

I think it would be much easier then to do this

val gather = Gather(url.openOrVoid)

where the *openOrVoid would just be left out in case of None. But this is not possible.

So you really should think about who is going to use your API and how they are likely to use it. It may well be that your users already use Option to store all variables for the very reason that they know they are optional in the end…

Defaulted parameters are nice but they also complicate things; especially when there is already an Option type around. I think there is some truth in your second question.

Debilski
  • 66,976
  • 12
  • 110
  • 133
  • Very good points, especially about what can be assumed about how the API users declare their arguments before calling the API. – DaGGeRRz Nov 17 '10 at 00:25
  • Is the default argument part of the API? If not, it would be bad practice to pass in None when you really mean "I don't care". What if the default changes to `Some(thingElse)`? – Aaron Novstrup Nov 17 '10 at 02:00
1

Might I just argue in favor of your existing approach, Some("callbackUrl")? It's all of 6 more characters for the API user to type, shows them that the parameter is optional, and presumably makes the implementation easier for you.

pr1001
  • 21,727
  • 17
  • 79
  • 125
  • All arguments are optional, but this particular parameter will be set to None if not specified. :) – DaGGeRRz Nov 16 '10 at 22:34
  • DaGGeRRz, I don't understand why that's a issue. As an API user, the first sample you gave was completely understandable. I have no problems calling it with Some(url). Seems like you're trying to avoid a problem that's not a problem. – James Moore Oct 26 '11 at 21:53
1

I think you should bite the bullet and go ahead with Option. I have faced this problem before, and it usually went away after some refactoring. Sometimes it didn't, and I lived with it. But the fact is that a default parameter is not an "optional" parameter -- it's just one that has a default value.

I'm pretty much in favor of Debilski's answer.

Community
  • 1
  • 1
Daniel C. Sobral
  • 295,120
  • 86
  • 501
  • 681
-1

I was also surprised by this. Why not generalize to:

implicit def any2Option[T](x: T): Option[T] = Some(x)

Any reason why that couldn't just be part of Predef?

Ben Jackson
  • 90,079
  • 9
  • 98
  • 150
  • 3
    Yes - it is a terrible idea, as has been pointed out many times. – oxbow_lakes Nov 16 '10 at 22:10
  • 1
    There are many reasons, not least that you would end up with `Some(null)` littering your program, and that the whole point of having an `Option` is type-safety (i.e. it is not interchangeable with its contents) – oxbow_lakes Nov 16 '10 at 22:19
  • As I mention in the comment above, I'd like to avoid implicit conversions. And I'm also interested in the "broader picture" of API design with named default arguments. :) – DaGGeRRz Nov 16 '10 at 22:25