选项和命名默认参数就像 Scala API 中的油和水吗?
我正在开发一个 Scala API(顺便说一句,适用于 Twilio),其中的操作具有相当大量的参数,并且其中许多参数都有合理的默认值。为了减少输入并提高可用性,我决定使用带有命名参数和默认参数的案例类。例如,对于 TwiML Gather 动词:
case class Gather(finishOnKey: Char = '#',
numDigits: Int = Integer.MAX_VALUE, // Infinite
callbackUrl: Option[String] = None,
timeout: Int = 5
) extends Verb
这里感兴趣的参数是callbackUrl。它是唯一真正可选的参数,因为如果没有提供任何值,则不会应用任何值(这是完全合法的)。
我已将其声明为一个选项,以便在 API 的实现端使用它执行单子映射例程,但这给 API 用户带来了一些额外的负担:
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)
据我所知,有两个选项(没有双关语)来解决这个问题。要么让 API 客户端将隐式转换导入作用域:
implicit def string2Option(s: String) : Option[String] = Some(s)
或者我可以重新声明具有 null 默认值的案例类,并将其转换为实现端的选项:
case class Gather(finishOnKey: Char = '#',
numDigits: Int = Integer.MAX_VALUE,
callbackUrl: String = null,
timeout: Int = 5
) extends Verb
我的问题如下:
- 有没有更优雅的方法来解决我的特定问题案件?
- 更一般地说:命名参数是一项新的语言功能 (2.8)。难道选项和命名默认参数就像油和水一样吗? :)
- 在这种情况下使用空默认值可能是最佳选择吗?
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:
- Are there any more elegant ways to solve my particular case?
- 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? :)
- Might using a null default value be the best choice in this case?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
就我个人而言,我认为在这里使用“null”作为默认值是完全可以的。当您想向客户传达某些内容可能未定义时,请使用 Option 而不是 null。因此,返回值可以声明为 Option[...],或者抽象方法的方法参数。这可以让客户免于阅读文档,或者更可能的是,因为没有意识到某些内容可能为空而获得 NPE。
就您而言,您知道可能存在空值。如果您喜欢 Option 的方法,只需在方法的开头执行
val optionFallbackUrl = Option(fallbackUrl)
即可。但是,此方法仅适用于 AnyRef 类型。如果您想对任何类型的参数使用相同的技术(不会导致 Integer.MAX_VALUE 作为 null 的替代品),那么我想您应该选择其他答案之一
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
我认为,只要 Scala 中没有语言支持真正的 void(解释如下)“类型”,从长远来看,使用 Option 可能是更干净的解决方案。甚至可能对于所有默认参数。
问题是,使用你的 API 的人知道你的一些参数是默认的,也可能将它们作为可选参数处理。因此,他们宣称它们
一切都很好,很干净,他们可以等待,看看是否能获得任何信息来填写此选项。
当最终使用默认参数调用 API 时,他们将面临一个问题。
我认为这样做会容易得多
,在
None
的情况下,将*openOrVoid
省略。但这是不可能的。因此,您确实应该考虑谁将使用您的 API 以及他们可能如何使用它。您的用户很可能已经使用
Option
来存储所有变量,因为他们知道它们最终是可选的......默认参数很好,但它们也使事情变得复杂;特别是当周围已经存在
Option
类型时。我认为你的第二个问题有一定道理。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
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.
I think it would be much easier then to do this
where the
*openOrVoid
would just be left out in case ofNone
. 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.我可以支持您现有的方法吗,
Some("callbackUrl")
? API 用户需要输入 6 个字符,向他们表明该参数是可选的,并且可能会让您更轻松地实现。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.我认为你应该硬着头皮继续使用
Option
。我以前遇到过这个问题,通常在进行一些重构后它就会消失。有时情况并非如此,但我还是忍受了。但事实是默认参数不是“可选”参数——它只是一个具有默认值的参数。我非常赞成Debilski的 答案。
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.
我也对此感到惊讶。为什么不概括为:
有什么理由不能只是 Predef 的一部分吗?
I was also surprised by this. Why not generalize to:
Any reason why that couldn't just be part of Predef?
这是另一个解决方案,部分灵感来自 克里斯的回答。它还涉及到一个包装器,但包装器是透明的,你只需要定义一次,API的用户不需要导入任何转换:
为了解决更大的设计问题,我不认为交互乍一看,选项和命名默认参数之间的关系就像油和水一样。可选字段和具有默认值的字段之间有明确的区别。可选字段(即
Option[T]
类型之一)可能永远没有值。另一方面,具有默认值的字段不需要将其值作为参数提供给构造函数。因此,这两个概念是正交的,并且字段可以是可选的并且具有默认值也就不足为奇了。也就是说,我认为对于此类字段使用
Opt
而不是Option
是合理的,而不仅仅是节省客户端的一些输入。这样做可以使 API 更加灵活,因为您可以将T
参数替换为Opt[T]
参数(反之亦然),而不会破坏该函数的调用者构造函数[1]。至于对公共字段使用
null
默认值,我认为这是一个坏主意。 “您”可能知道您期望一个null
,但访问该字段的客户端可能不知道。即使该字段是私有的,当其他开发人员必须维护您的代码时,使用null
也会带来麻烦。所有关于null
值的常见争论都在这里发挥作用 - 我不认为这个用例有任何特殊的例外。[1] 前提是您删除了 option2opt 转换,以便每当需要
Opt[T]
时调用者都必须传递T
。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:
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 thanOption
for such fields, beyond just saving the client some typing. Doing so makes the API more flexible, in the sense that you can replace aT
argument with anOpt[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 anull
, but clients that access the field may not. Even if the field is private, using anull
is asking for trouble down the road when other developers have to maintain your code. All the usual arguments aboutnull
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 anOpt[T]
is required.不要将任何内容自动转换为选项。使用我的答案,我认为你可以很好地做到这一点,但在类型安全中方式。
然后您可以根据需要调用它 - 显然将您的行为方法适当地添加到
FallbackUrl
和NumDigits
中。这里的主要缺点是它是大量的样板文件Don't auto-convert anything to an Option. Using my answer here, I think you can do this nicely but in a typesafe way.
Then you can call it as you wanted to - obviously adding your behaviour methods to
FallbackUrl
andNumDigits
as appropriate. The main negative here is that it is a ton of boilerplate