在Scala中,我可以通过隐式转换引用私有类型

在Scala中,我可以通过隐式转换引用私有类型,scala,jodatime,reactive-programming,Scala,Jodatime,Reactive Programming,我在nscala_时间包(joda time的scala版本)中发现了这种有趣的行为 import com.github.nscala\u time.time.Imports_ 导入com.github.nscala\u time.time.DurationBuilder 对象测试{ val x=3秒 //>x:类型为com.github.nscala_time.time.DurationBuilder val xx:DurationBuilder=3秒 //>未能编译: //无法在包com.g

我在nscala_时间包(joda time的scala版本)中发现了这种有趣的行为

import com.github.nscala\u time.time.Imports_
导入com.github.nscala\u time.time.DurationBuilder
对象测试{
val x=3秒
//>x:类型为com.github.nscala_time.time.DurationBuilder
val xx:DurationBuilder=3秒
//>未能编译:
//无法在包com.github.nscala\u time.time中访问包时间中的类DurationBuilder
}
我试图实现的是从nscala_time Duration到scala.concurrent.Duration的隐式转换 我需要这个,因为我在一个应用程序中使用RxScala和nscala_时间

//例如,应隐式转换以下内容
//先到nscala_持续时间
//而不是scala.lang.concurrent.Duration
3秒
nscala_time为我的应用程序提供了丰富的时间和日期api,而我在同一个类中使用RxScala实现GUI响应。
您可以下载一个简单的项目来玩:

也许您可以使用隐式转换?(顺便说一句,nscala中的持续时间本质上是
org.joda.time.Duration
):


(不使用
导入scala.concurrent.duration.\u
此处避免与joda/nlscala内容的名称冲突)

来自集团:这是一个已知的问题,这实际上是我尝试做的。然而,当你写“3秒”时,你会得到一个DurationBuilder(不是org.joda.time.Duration)。因此隐式转换将不适用。我已经准备了一个简单的项目,这样你就可以玩了:你能发布一个片段吗?我唯一能让它工作的方法是:val x:ScalaDuration=(3秒)。toDuration但它远不是优雅的解决方案是的,它比我想象的稍微复杂一些。要更新answernscala_时间不是一件好事,如果它没有帮助,那么就放弃它,直接使用org.joda.time.Duration(这是我在类似情况下所做的)
scala> import com.github.nscala_time.time.Imports._
import com.github.nscala_time.time.Imports._

scala> implicit class DurationHelper(d:org.joda.time.Duration) {
     | def toScalaDuration = scala.concurrent.duration.Duration.apply(d.getMillis,scala.concurrent.duration.MILLISECONDS)
     | }
defined class DurationHelper

scala> val d = RichInt(3).seconds.toDuration
// toDuration method is defined for com.github.nscala_time.time.DurationBuilder
d: org.joda.time.Duration = PT3S

scala> def exfun(d:scala.concurrent.duration.Duration) = d.toString
exfun: (d: scala.concurrent.duration.Duration)String

scala> exfun(d)
res41: String = 3000 milliseconds