在 Scala 文档中搜索 #::

发布于 2024-10-09 13:43:23 字数 313 浏览 0 评论 0原文

我正在尝试查找 Scala 运算符方法 #:: 的文档。我相信它是在 Stream 类中定义的,因为 示例 我发现使用它。

我的问题并不是特定于此方法(尽管我想知道文档在哪里),而是如何搜索一般的 Scala 文档。我尝试在文档页面(2.8.1)左上角的搜索框中输入 #::,但什么也没找到。

I am trying to find the documentation for the Scala operator method #::. I believe that it is defined in the Stream class because of an example I found that uses it.

My question is not particular to this method (although I would like to know where the docs are), but how to search the Scala docs in general. I tried entering #:: in the search box in the upper left of the documentation page (2.8.1), but found nothing.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

皇甫轩 2024-10-16 13:43:23

我建议使用 参考索引 - 它专门设计用于查找任何类型的符号(类、特征、方法、值、变量),无论其层次位置如何 - 与 Scaladoc 的左侧索引形成对比,后者不显示内部类、特征或物体。

不幸的是,它只在夜间提供。您可以在 nightly 查看整个内容斯卡拉多克。请注意左框架中索引上方的上部框。

希望它能与 Scala 2.9.0 捆绑在一起。

编辑 从 2.9.0 开始,参考索引开始与 Scaladoc。现在无需查看夜间文档。

I suggest using the Reference Index - it's designed specifically to look for any kind of symbol (class, traits, methods, vals, vars) regardless of it's hierarchical position - contrasting with the Scaladoc's left index which doesn't show inner classes, traits or objects.

Unfortunately it's only available in the nightly. You can see the whole thing at nightly Scaladoc. Notice the upper box in the left frame, above the index.

Hope it will be bundled with Scala 2.9.0.

Edit As of 2.9.0, the reference index started to be bundle with Scaladoc. No need to go to the nightly docs now.

无边思念无边月 2024-10-16 13:43:23

正如其他人已经提到的,#:: 是在 scala.collection.immutable.Stream.ConsWrapper。我只是想花一点时间来详细说明原因。

一般来说,要调用对象上的运算符,该对象需要存在。然而,流的想法是直到需要时才评估流的尾部。因此,请考虑以下流:

def fibs(a:Int,b:Int):Stream[Int] = a #:: fibs(b,a+b)

通常,我们需要评估递归 fibs 调用,以便我们可以对其调用 #:: 运算符。这会导致递归失控。这不是我们想要的。我们想要的是接收者是一个名为Stream的接收者。因此,ConsWrapper

ConsWrapper 的构造函数是 class ConsWrapper[T](tail: => Stream[T]),采用 by-名称 Stream,它是通过隐式转换 Stream.consWrapper[T](stream: => Stream[T]) 创建的,该转换还采用名称 <代码>流。

因此,我们对尚未调用的函数的结果执行了隐式转换,并且模仿了使用名称 this#:: 的效果代码>参考。

As others have already mentioned, #:: is defined on scala.collection.immutable.Stream.ConsWrapper. I just wanted to take a minute to elaborate on why that is.

In general, to call an operator on an object, that object needs to exist. However, the idea with a Stream is the tail of the stream is not evaluated until it needs to be. So consider the following stream:

def fibs(a:Int,b:Int):Stream[Int] = a #:: fibs(b,a+b)

Ordinarily, we would need to evaluate the recursive fibs call so that we could call the #:: operator on it. This would lead to runaway recursion. This is NOT what we want. What we want is for the reciever to be a by-name Stream. Hence the ConsWrapper:

The constructor for ConsWrapper is class ConsWrapper[T](tail: => Stream[T]) taking a by-name Stream, and it's created through an implicit conversion Stream.consWrapper[T](stream: => Stream[T]), which also takes a by-name Stream.

Hence, we have performed an implicit conversion on the result of a function that has not yet been called, and we have mimiced the effect of calling #:: with a by-name this reference.

相对绾红妆 2024-10-16 13:43:23

这里的问题是 scaladoc 搜索不允许您查找内部类/对象(即其父类不是包)。 #:: 的声明是 Stream.#::Stream.ConsWrapper.#::

object Stream { 
  //STUFF
  /** An extractor that allows to pattern match streams with `#::`.
   */
  object #:: {
    def unapply[A](xs: Stream[A]): Option[(A, Stream[A])] = 
      if (xs.isEmpty) None
      else Some((xs.head, xs.tail))
  }
  class ConsWrapper[A](tl: => Stream[A]) {
    def #::(hd: A): Stream[A] = new Stream.Cons(hd, tl)
    def #:::(prefix: Stream[A]): Stream[A] = prefix append tl
  }
  //MORE STUFF
}

您可以将其作为 RFE 请求到 trac 中的 scaladoc 工具。

在 IntelliJ IDEA 的 scala 插件中,您可以使用符号查找 (CTRL+ ALT+ SHIFT+ N)并输入 #:: ,这将立即显示 #:: 的两个声明。

The problem here is that the scaladoc search does not allow you to look for an inner class/object (i.e. whose parent is not a package). The declaration of #:: is either Stream.#:: or Stream.ConsWrapper.#:::

object Stream { 
  //STUFF
  /** An extractor that allows to pattern match streams with `#::`.
   */
  object #:: {
    def unapply[A](xs: Stream[A]): Option[(A, Stream[A])] = 
      if (xs.isEmpty) None
      else Some((xs.head, xs.tail))
  }
  class ConsWrapper[A](tl: => Stream[A]) {
    def #::(hd: A): Stream[A] = new Stream.Cons(hd, tl)
    def #:::(prefix: Stream[A]): Stream[A] = prefix append tl
  }
  //MORE STUFF
}

You could request this as an RFE to the scaladoc tool in trac.

In IntelliJ IDEA's scala plugin, you could have used symbol lookup (CTRL+ ALT+ SHIFT+ N) and typed #:: and this would have brought up both declarations of #:: immediately.

杯别 2024-10-16 13:43:23

该特定方法在 Stream 内部的嵌套类中定义,称为 scala.collection.immutable.Stream.ConsWrapper

不,我完全不知道如何找到它。我只是偶然发现的。尽管我现在知道在哪里可以找到它,但当我想在答案中发布该课程的链接时,我仍然第一次找不到它(甚至第二次和第三次)尝试。

That particular method is defined in a nested class inside of Stream, called scala.collection.immutable.Stream.ConsWrapper.

And no, I have absolutely no idea how one would go about finding it. I only stumbled across it by accident. And even though I knew where to find it now, when I wanted to post the link to the class here in my answer, I still couldn't find it on the first (and even second and third) try.

浮华 2024-10-16 13:43:23

好吧,通常情况下,如果我们看到

foo bar baz 

bar 是一个为 foo 定义的方法,那么我们首先查看类/对象 - foo 的定义,然后向上查看继承/特征树(+ 在与 foo 的隐式转换中,在当前文件,以及(直接)包含的文件中)。

除了“bar”以冒号结尾之外,这里就是这种情况。那么就要以相反的顺序读取——

foo bar: baz 

不是

foo.bar: (baz)

,而是

baz.bar: (foo) 

所以我们必须按照上面描述的方式查找,但不是查找 foo,而是查找 baz。

Well, normally, if we see

foo bar baz 

then bar is a method, defined for foo, so we first look in the class/object - definition of foo, then the inheritance/trait tree upwards (+ in implicit conversions to and from foo, in the current file, and in (directly) included files).

Except 'bar' ends in a colon, which is the case here. Then it is to be read in reverse order -

foo bar: baz 

is not

foo.bar: (baz)

, but

baz.bar: (foo) 

So we have to look up in the way described above, but not for foo, but for baz.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文