Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a naming convention for implicit classes?

Tags:

scala

For example in Objective-C, where class extensions are direct language construct, we do:

in filed named NSArray+FKBinarySearch.h:

@interface NSArray (FKBinarySearch)
// ...
@end

Do you name your extension classes like:

implicit class IndexedSeqBinarySearch[A](seq: IndexedSeq[A]) {
  // ...
}

For example scalaz calls makes extensions classes with suffix Ops: BooleanOps or even extending their own traits FoldableOps

Yet Ops feels too general, OTOH there are packages. Also Ops in scalaz case contains everything.

like image 625
phadej Avatar asked Jan 21 '15 07:01

phadej


1 Answers

In Programming in Scala in "Chapter 21 · Implicit Conversions and Parameters" -> "Naming an implicit conversion." it is said:

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.

In Programming Scala "Chapter 5: Implicits" -> "Wise Use of Implicits" it is said:

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.

Using implicit class is like using implicit method, but in case of class its primary constructor is involved. And if you look at Scala implicit conversion methods results you can find something like:

  • wrapString(s: String): WrappedString
  • genericArrayOps[T](xs: Array[T]): ArrayOps[T]

In my opinion inside Implicits object for your implicit class name the following can be used:

  • xOps
  • RichX
  • WrappedX
  • AsX

You may choose any of them which is appropriate to your project conditions.

like image 195
user5102379 Avatar answered Sep 27 '22 17:09

user5102379