这段代码在 scala 中到底做了什么?

发布于 2024-10-20 03:54:05 字数 398 浏览 1 评论 0原文

我试图理解 twitter gizzard 示例 rowz,但我无法弄清楚这个小片段在 scala 中的作用:

package com.twitter.rowz
import com.twitter.gizzard.nameserver.{Forwarding, NameServer}
import com.twitter.gizzard.shards.ShardException

class ForwardingManager(nameServer: NameServer[Shard]) extends (Long => Shard) {  
    def apply(id: Long) = nameServer.findCurrentForwarding(0, id)
}

类到底扩展了什么?

I'm trying to understand the twitter gizzard example rowz, and I can't figure out what this little snippet does in scala:

package com.twitter.rowz
import com.twitter.gizzard.nameserver.{Forwarding, NameServer}
import com.twitter.gizzard.shards.ShardException

class ForwardingManager(nameServer: NameServer[Shard]) extends (Long => Shard) {  
    def apply(id: Long) = nameServer.findCurrentForwarding(0, id)
}

what exactly is the class extending?

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

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

发布评论

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

评论(2

梦途 2024-10-27 03:54:05

(A=>B)Function1[A,B]

这些行是严格等效的:

class ForwardingManager(nameServer: NameServer[Shard]) extends (Long => Shard)
class ForwardingManager(nameServer: NameServer[Shard]) extends Function1[Long,Shard]

(A=>B) is Function1[A,B]

Those lines are strictly equivalent:

class ForwardingManager(nameServer: NameServer[Shard]) extends (Long => Shard)
class ForwardingManager(nameServer: NameServer[Shard]) extends Function1[Long,Shard]
冷血 2024-10-27 03:54:05

此代码段定义了扩展 Function1[Long, Shard] 的类 ForwardingManagerLong => Shard 只是缩写形式)。

作为另一个示例,您可以查看 Scala 库中的 Parser 类:

https://github.com/scala/scala/blob/master/src/library/scala/util/parsing/combinator/Parsers.scala#L190< /a>

在这种情况下 (Input => ParseResult[T])Function1[Input, ParseResult[T]] 相同。 Programming in Scala, 2nd Edition(第 33.6 节)中也描述了这个具体示例

This snippet defines class ForwardingManager that extends Function1[Long, Shard] (Long => Shard is just short form).

As another example, you can look at Parser class from Scala library:

https://github.com/scala/scala/blob/master/src/library/scala/util/parsing/combinator/Parsers.scala#L190

In ths case (Input => ParseResult[T]) is the same as Function1[Input, ParseResult[T]]. This concrete example is described also in Programming in Scala, 2nd Edition (section 33.6)

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