10

当我返回 type 的东西时Option,在函数名的名称中解释它是一个选项,而不是东西本身似乎很有用。例如,seqs 有reduceOption. 有标准的命名约定吗?我见过的东西:

maybeFunctionName

functionNameOption

- 两者似乎都不是那么好。

4

2 回答 2

16

reduceOption and friends (headOption, etc.) are only named that way to distinguish them from their unsafe alternatives (which arguably shouldn't exist in the first place—i.e, there should just be a head that returns an Option[A]).

whateverOption isn't the usual practice in the standard library (or most other Scala libraries that I'm aware of), and in general you shouldn't need or want to use this kind of Hungarian notation in Scala.

于 2012-08-09T16:00:18.107 回答
8

为什么要使函数名称更长?它没有任何贡献,因为它返回一个 Option 的事实在查看函数的类型时是显而易见的。

reduceOption 是一种特殊情况,因为在大多数情况下,您确实想使用 reduce,但它不适用于空序列。

于 2012-08-09T16:00:45.267 回答