Reputation: 331
While going through Spray.io examples library I came across this declaration of val
in FileUploadHandler example of routing app.
val Some(HttpHeaders.Content-Type(ContentType(multipart: MultipartMediaType, _))) = header[HttpHeaders.Content-Type]
As per my understanding the variable declaration goes as val <identifier> = ...
Please help in understanding this paradigm of syntax.
Upvotes: 2
Views: 212
Reputation: 2735
val
is a bit more complex than just an assignment operator.
A definition
val p = e
where p
is not just a variable name, is expanded to
val x = e match { case p => x }
Take a loot at the simplest example:
val Some(s) = Some(5)
As a result, s
would be equal 5.
In your example header[HttpHeaders.Content-Type]
is matched against Some(...).
Upvotes: 2
Reputation: 6059
According to Scala language spec: Value definitions can alternatively have a pattern as left-hand side. Watch out for PatDef
in the document.
Section "Patterns in Value Definitions" of Daniel Westheide's Blog gives a nice overview on the usage.
Upvotes: 1
Reputation: 424
You're looking for extractors/mattern matching in scala, please see http://www.scala-lang.org/old/node/112.
You need a simple form of it, take a look at this snippet:
scala> val Some(t) = Some("Hello")
t: String = Hello
Upvotes: 0