是否有隐式 类 的命名约定?
Is there a naming convention for implicit classes?
例如在 Objective-C 中,class 扩展是直接语言结构,我们这样做:
在名为 NSArray+FKBinarySearch.h 的文件中:
@interface NSArray (FKBinarySearch)
// ...
@end
您是否将您的分机 class 命名为:
implicit class IndexedSeqBinarySearch[A](seq: IndexedSeq[A]) {
// ...
}
例如,scalaz 调用使用后缀 Ops
扩展 classes:BooleanOps or even extending their own traits FoldableOps
不过Ops
感觉太笼统了,OTOH还有套餐。此外 Ops
在 scalaz 案例中包含一切。
标准库有StringOps
.
邮件列表中的某个人让我转向“荒谬的描述性反引号标识符”,以避免隐式命名冲突。
OTOH,也许您想通过隐藏它让客户轻松关闭隐式。
在Programming in Scala中"Chapter 21 · Implicit Conversions and Parameters" -> "Naming an implicit conversion."中说:
Implicit conversions can have arbitrary names. The name of an implicit
conversion matters only in two situations: if you want to write it
explicitly in a method application, and for determining which implicit
conversions are available at any place in the program.
在Programming Scala "Chapter 5: Implicits" -> "Wise Use of Implicits"中说:
One way to improve implicits visibility is to adopt the practice of
putting implicit values in a special package named implicits or an
object named Implicits.
使用隐式 class 就像使用隐式方法,但在 class 的情况下涉及其主构造函数。如果您查看 Scala 隐式转换方法结果,您会发现类似以下内容:
- wrapString(s: String): WrappedString
- genericArrayOps[T](xs: Array[T]): ArrayOps[T]
在我看来 Implicits
对象内的隐式 class 名称可以使用以下内容:
- xOps
- RichX
- WrappedX
- AsX
您可以选择适合您的项目条件的任何一种。
不,实际上并没有既定的惯例。 @srgfed01 列出了一些方法(我在野外看到的其他约定:在名为 ImplicitX
而不是 Implicits
的对象中对 X
的隐式转换,以及对隐式 PimpedX
class 包装 X
),但您会在现存的 Scala 库中看到所有这些以及更多内容——有时在同一个库中有多个不同的约定。对于隐式 转换 ,没有什么比 x2y
风格更成熟的了,也许是因为隐式 classes 是一个相对较新的功能。
所以选择你喜欢的风格,并准备好在你使用的库中寻找许多不同约定下的隐式。
例如在 Objective-C 中,class 扩展是直接语言结构,我们这样做:
在名为 NSArray+FKBinarySearch.h 的文件中:
@interface NSArray (FKBinarySearch)
// ...
@end
您是否将您的分机 class 命名为:
implicit class IndexedSeqBinarySearch[A](seq: IndexedSeq[A]) {
// ...
}
例如,scalaz 调用使用后缀 Ops
扩展 classes:BooleanOps or even extending their own traits FoldableOps
不过Ops
感觉太笼统了,OTOH还有套餐。此外 Ops
在 scalaz 案例中包含一切。
标准库有StringOps
.
邮件列表中的某个人让我转向“荒谬的描述性反引号标识符”,以避免隐式命名冲突。
OTOH,也许您想通过隐藏它让客户轻松关闭隐式。
在Programming in Scala中"Chapter 21 · Implicit Conversions and Parameters" -> "Naming an implicit conversion."中说:
Implicit conversions can have arbitrary names. The name of an implicit conversion matters only in two situations: if you want to write it explicitly in a method application, and for determining which implicit conversions are available at any place in the program.
在Programming Scala "Chapter 5: Implicits" -> "Wise Use of Implicits"中说:
One way to improve implicits visibility is to adopt the practice of putting implicit values in a special package named implicits or an object named Implicits.
使用隐式 class 就像使用隐式方法,但在 class 的情况下涉及其主构造函数。如果您查看 Scala 隐式转换方法结果,您会发现类似以下内容:
- wrapString(s: String): WrappedString
- genericArrayOps[T](xs: Array[T]): ArrayOps[T]
在我看来 Implicits
对象内的隐式 class 名称可以使用以下内容:
- xOps
- RichX
- WrappedX
- AsX
您可以选择适合您的项目条件的任何一种。
不,实际上并没有既定的惯例。 @srgfed01 列出了一些方法(我在野外看到的其他约定:在名为 ImplicitX
而不是 Implicits
的对象中对 X
的隐式转换,以及对隐式 PimpedX
class 包装 X
),但您会在现存的 Scala 库中看到所有这些以及更多内容——有时在同一个库中有多个不同的约定。对于隐式 转换 ,没有什么比 x2y
风格更成熟的了,也许是因为隐式 classes 是一个相对较新的功能。
所以选择你喜欢的风格,并准备好在你使用的库中寻找许多不同约定下的隐式。