简体   繁体   English

如何在Scala中找到用于时间转换隐式的包?

[英]How do I find the package for the time conversion implicits in Scala?

On the official API page, I've searched for "time", but can't find the class. 在正式的API页面上,我已经搜索了“时间”,但是找不到该类。 I looked at all methods on RichLong and RichInt , but can't find the methods. 我查看了RichLongRichInt上的所有方法,但找不到这些方法。

I'm specifically talking about the methods that convert between int/long to some kind of rich object: 我特别在谈论在int / long和某种富对象之间转换的方法:

2 hours + 12 seconds

Note I'm not asking what the package is, I want to know how to find it. 注意,我并不是在问软件包是什么 ,我想知道如何找到它。

您可以使用-Xlog-implicits标志使编译器向您显示查找隐式转换的位置。

Those aren't in the standard Scala API. 这些不在标准的Scala API中。 Are you using the scala-time wrapper for JodaTime? 您是否在JodaTime中使用scala时间包装器? If so, that would tell you where to look. 如果是这样,那会告诉您在哪里看。 In general, if you know which import enables the capability, it helps a lot when trying to find documentation! 通常,如果您知道哪个导入启用了此功能,那么在尝试查找文档时它将大有帮助!

If you know a method name, you can click on the little letters at the top of the left panel, just below the search text field in ScalaDoc--this will bring up a list of everything in the docs with that name, including methods (and tell you how to find it). 如果您知道方法名称,则可以单击左面板顶部位于ScalaDoc中搜索文本字段下方的小写字母-这将显示该名称的文档中所有内容的列表,包括方法(并告诉您如何找到它)。

If a class doesn't have a method itself, you can use Scala to tell you what class it's getting converted to: 如果一个类本身没有方法,则可以使用Scala告诉您它将转换为哪个类:

def whoHasSize[A](a: A)(implicit ev: A => { def size: Int }) = ev(a).getClass.getName

scala> whoHasSize("fish")
res1: java.lang.String = scala.collection.immutable.StringOps

So here you see that the string "fish" does not itself have a size method; 因此,在这里您可以看到字符串"fish"本身没有size方法。 instead, that's been granted by the scala.collection.immutable.StringOps method. 而是由scala.collection.immutable.StringOps方法授予的。 You could do the same thing to find out about seconds . 您可以做同样的事情来找出大约seconds

Finally, stew's answer is probably what you were really looking for! 最后,炖煮的答案可能正是您真正想要的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM