Norm
Norm

Reputation: 23

What exactly does this snippet do in scala?

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?

Upvotes: 2

Views: 243

Answers (2)

tenshi
tenshi

Reputation: 26566

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)

Upvotes: 1

shellholic
shellholic

Reputation: 6114

(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]

Upvotes: 6

Related Questions