编写了一些scala工具后,我正试图掌握安排代码的最佳方法 - 特别是暗示.我有2个目标:
为了避免重复隐含,我提出了这种结构(类似于scalaz的排列方式):
case class StringW(s : String) {
def contrived = s + "?"
}
trait StringWImplicits {
implicit def To(s : String) = StringW(s)
implicit def From(sw : StringW) = sw.s
}
object StringW extends StringWImplicits
// Elsewhere on Monkey Island
object World extends StringWImplicits with ListWImplicits with MoreImplicits
Run Code Online (Sandbox Code Playgroud)
这让我只是
import StringW._ // Selective import
Run Code Online (Sandbox Code Playgroud)
或(在大多数情况下)
import World._. // Import everything
Run Code Online (Sandbox Code Playgroud)
其他人如何做到这一点?
scala ×1