小编Nic*_*dge的帖子

我应该如何在Scala应用程序中组织implicits?

编写了一些scala工具后,我正试图掌握安排代码的最佳方法 - 特别是暗示.我有2个目标:

  • 有时候,我希望能够只输入我要求的含义.
  • Othertimes,我想进口一切.

为了避免重复隐含,我提出了这种结构(类似于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

11
推荐指数
1
解决办法
472
查看次数

标签 统计

scala ×1