According to the Play 2.0 documentation, pattern matching can be done in a template like so:
@connected match {
case models.Admin(name) => {
<span class="admin">Connected as admin (@name)</span>
}
case models.User(name) => {
<span>Connected as @name</span>
}
}
The text between the brackets after the case expressions is treated as output (e.g. HTML), and this is quite convenient.
However, when attempting to use a match expression that is not a simple variable, such as object.member, like this:
@album.year match {
case Some(y: Int) => { @y }
case None => { <b>nope</b> }
}
it results in a compilation error:
"')' expected but 'case' found."
Using defining to bind the expression to a simple variable, like this:
@defining(album.year) { foo =>
@foo match {
case Some(y: Int) => { @y }
case None => { <b>nope</b> }
}
}
works, but it seems a bit cumbersome.
Is there a proper way to use this pattern matching feature on expressions that involve an object and a member (e.g. album.year)?
Not currently possible (in version 2.0.1), as it is a confirmed bug:
https://play.lighthouseapp.com/projects/82401/tickets/46-support-more-complex-match-statement