Scala parameters pattern (Spray routing example)
scala, shapeless, spray
Solution
Senia's answer is helpful in understanding the Spray-routing directives and how they use HLists to do their work. But I get the impression you were really just interested in the Scala constructs used in
path( "foo" / Segment / Segment ) { (a,b) => ... }
It sounds as though you are interpreting this as special Scala syntax that in some way connects those two `Segment` instances to `a` and `b`. That is not the case at all.
path( "foo" / Segment / Segment )
is just an ordinary call to `path` with a single argument, an expression involving two calls to a `/` method. Nothing fancy, just an ordinary method invocation.
The result of that call is a function which wants another function -- the thing you want to happen when a matching request comes in -- as an argument. That's what this part is:
{ (a,b) => ... }
It's just a function with two arguments. The first part (the invocation of `path`) and the second part (what you want done when a matching message is received) are not syntactically connected in any way. They are completely separate to Scala. However, Spray's semantics connects them: the first part creates a function that will call the second part when a matching message is received.
Problem
Sorry about the vague title...wasn't sure how to characterize this. I've seen/used a certain code construction in Scala for some time but I don't know how it works. It looks like this (example from Spray routing): ``` path( "foo" / Segment / Segment ) { (a,b) => { // <-- What's this style with a,b? ... }} ``` In this example, the Segements in the path are bound to a and b respectively inside the associated block. I know how to use this pattern but how does it work? Why didn't it bind something to "foo"? I'm not so interested in how spray works for my purpose here, but what facility of Scala is this, and how would I write my own?