Reputation: 2385
@(css: Option[String])(content: Html)(customJS: Option[Array[String]])
@for(js <- customJS if !customJS.isEmpty) {
<script src="@routes.Assets.at(js)"></script>
}
I was writing for this small for-comprehension that will loop through and print out the uri String. Since it is possible to be none, or an array of Strings, I used Option
.
Then the complier from typesafe activator told me that @routes.Assets.at()
is supposed to take a String
not an Array[String]
. I'm in shock because js is supposed to be String
right?
Then I tried to write like this:
@for(js <- customJS if !customJS.isEmpty) {
@var uri = js
<script src="@routes.Assets.at(js)"></script>
}
Then the complier said that added line is illegal start of "simple expression".
It is not a simple expression! It is a full for-comprehension with {}
! Also I was eventually forced to use js.asInstanceOf[String]
to fool the complier. I'm not glad with this because once I was told that I should never use asInstanceOf
. So what's wrong with my original code?
Upvotes: 1
Views: 73
Reputation: 38045
Note that you should not use Array
- use Seq
.
There is no need in Option[Seq[String]]
- use Seq[String]
. Instead of None
you could always use an empty Seq
.
In case you still want to use Option[Seq[String]]
you should rewrite your for
like this:
@for(jsArray <- customJS; js <- jsArray) {
<script src="@routes.Assets.at(js)"></script>
}
You don't need if !customJS.isEmpty
. Option
is something like collection with 1 or 0 elements. Element of Option[Array[String]]
is Array[String]
.
Upvotes: 1