2

How do I properly call exists for java.util.Enumeration? I cannot change the source, it's from HttpServletRequest.

This (unrelated) code works:

val a = List("One", "Two", "Three")
a exists (_ contains "T")

But why this code cannot be compiled:

import collection.JavaConversions.enumerationAsScalaIterator
enumerationAsScalaIterator(req.getHeaderNames[String])[String] exists (_ contains "T")

I'm using IntelliJ as my IDE. The above code doesn't show any error in the editor but will throw error when compiled:

error: ';' expected but '[' found. enumerationAsScalaIterator(req.getHeaderNames[String])[String] exists (_ contains "T")

4 Answers 4

5

Try

import collection.JavaConversions.enumerationAsScalaIterator

req.getHeaderNames() exists (_ contains "T")
Sign up to request clarification or add additional context in comments.

Comments

1

Your second [String] here: req.getHeaderNames[String])[String] Isn't needed, and isn't valid Scala. Try

enumerationAsScalaIterator(req.getHeaderNames[String]) exists (_ contains "T")

Better yet, enumerationAsScalaIterator is available implicitly, so you should be able to just do:

req.getHeaderNames exists (_ contains "T")

1 Comment

Same with above, it produces "Cannot resolve symbol contains" in the IDE.
1

It should be enumerationAsScalaIterator[String](req.getHeaderNames[String]) exists (_ contains "T") (or use it implicitly, as Pablo Fernandez suggests). No idea why it wouldn't show an error in the editor, however.

Comments

1

Looks like the only way to do it for HttpServletRequest.getHeaderNames is the good old loop:

val headers = req.getHeaderNames
while (headers.hasMoreElements) {
if (headers.nextElement.asInstanceOf[String] contains "T")
  return FOO
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.