Hassan Syed
Hassan Syed

Reputation: 20485

Scala/akka Pattern Matching a Sequence of Futures pipelined to a actor message

Ok I'm trying to create a unit of work of web requests and pipe it to an actor when it's done. I don't understand the matching ins and outs well enough yet and i'm losing hair trying to come up with a way to do the match.

I've got a catch-all style of a match with case _ : Seq[_] how would I go about 1) getting a proper case class matcher, in this case Seq[SubcategoryResponse] and 2) how would do I get a handle to it inside the match body ?

val next_batch = Future.traverse(result.urls.get)(u => {
      val F = System.requester ? makeGetRequest(Root(),u.data)
      val F1 = F.map(f=> SubcategoryResponse(f.asInstanceOf[HttpResponse],u.payload.get,level+1))
      F1
    })
val res = Await result(next_batch, 20.seconds)
println(res.getClass)
res match {
  case _ : Seq[_] => {
    println("boom")
  }
}

edit: Bingo

Simple syntax :( -> m : Seq[SubcategoryResponse]. The compiler warning doesn't make me feel very comfortable though. So my new question is, what does type erasure mean in this context. Do i need to take that warning seriously ? If so, how do I get around it ?

[warn] /Users/hassan/code/scala/extractor/HonCrawler.scala:139: non-variable type argument HonCategoryBootstrap.this.SubcategoryResponse in type pattern Seq[HonCategoryBootstrap.this.SubcategoryResponse] is unchecked since it is eliminated by erasure
[warn]     case m : Seq[SubcategoryResponse] => {
[warn]              ^
[warn] one warning found

Upvotes: 0

Views: 1170

Answers (1)

sourcedelica
sourcedelica

Reputation: 24040

Instead of sending Seqs as messages, wrap the Seq in another class. For example:

case class SubcategoryResponses(responses: Seq[SubcategoryResponse])

Then you can match on the case class:

res match {
  case SubcategoryResponses(responses) => 

Upvotes: 2

Related Questions