我在nscala_time包中找到了这个有趣的行为(joda-time的scala版本)
import com.github.nscala_time.time.Imports._
import com.github.nscala_time.time.DurationBuilder
object tests {
val x = 3 seconds
//> x : is of type com.github.nscala_time.time.DurationBuilder
val xx: DurationBuilder = 3 seconds
//> fails to compile:
// class DurationBuilder in package time cannot be accessed in package com.github.nscala_time.time
}
我想要实现的是从nscala_time持续时间到scala.concurrent.Duration的隐式转换 我需要这个,因为我在一个应用程序中使用RxScala和nscala_time。
// e.g. the following should be implicitly converted
// to nscala_time Duration first
// than to scala.lang.concurrent.Duration
3 seconds
nscala_time提供丰富的时间和时间。我的应用程序的日期api,而我在同一类中使用RxScala进行GUI响应。 您可以下载一个简单的项目来玩:https://dl.dropboxusercontent.com/u/9958045/implicit_vs_private.zip
答案 0 :(得分:1)
也许你可以使用隐式转换? (btw Duration
在nscala中基本上是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
(这里不使用import scala.concurrent.duration._
来避免与joda / nlscala的名字冲突)
答案 1 :(得分:1)