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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…