struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andreas Joseph Krogh <andr...@officenet.no>
Subject Re: Scala and s:iterator
Date Mon, 25 May 2009 19:31:12 GMT
On Sunday 24 May 2009 09:36:48 pm Andreas Joseph Krogh wrote:
> Hi all.
> Normally this kind of post would be better posted to -users, but I'm looking for the
developers comments on this.
> 
> Anyone have any experience using Scala with Struts2? I'm especially thinking about s:iterate
and scala.List.
> 
> I've managed to use s:iterate with Scala's List like this:
> <s:iterator value="%{getMyList().toArray().unbox(@java.lang.Object@class)}" status="status">
> getMyList() returns a scala.List from my Scala-object, which I convert to an JAVA-array
using toArray().unbox().
> 
> This seems very hackish and it would be cool if one could make a "struts2-scala-plugin"
which registered some kind of Iterator which the IteratorComponent could use, refactoring
out the call to "MakeIterator.convert(findValue(value))" to a factory of some kind which checked
some kind of registry for providing a proper Iterator for the Scala-object returned by findValue(value).
> 
> Comments?

For the curious;
After some Googleing I "solved" this issue by defining a getListAsJavaIterator() method in
my Scala-object which uses "implicit conversion" compiler-magic to define a new "iterator()"
method on the Scala-Iterable Trait:

object JavaIterableConversions {

	class JavaIterator[T](itr: Iterator[T]) extends java.util.Iterator[T] {
		def hasNext() = itr.hasNext
		def next() = itr.next
		def remove() = throw new
				java.lang.UnsupportedOperationException("Remove is not implemented in JavaIterableConversions:JavaIterator")
	}

	class JavaIterable[T](iterable: Iterable[T]) extends java.lang.Iterable[T] {
		def iterator() = new JavaIterator[T](iterable.elements)
	}

	implicit def implicitScalaIterableToJavaIterable[T](iterable : Iterable[T]): java.lang.Iterable[T]
=
		new JavaIterable[T](iterable)

}

Usage:

import JavaIterableConversions._
class MyScalaClass {
	@BeanProperty
	var items: List[Item] = _

	def getListAsJavaIterator(): java.util.Iterator[_] = {
		items.iterator();
	}
}

Note: the iterator() *must* be defined in the same class as the implicit-conversion implicitScalaIterableToJavaIterable
is defined/imported as it's the scala-compiler which does it's magic here. Trying to call
items.iterator() from a JAVA-class will *not* work.

-- 
Andreas Joseph Krogh <andreak@officenet.no>
Senior Software Developer / CTO
------------------------+---------------------------------------------+
OfficeNet AS            | The most difficult thing in the world is to |
Rosenholmveien 25       | know how to do a thing and to watch         |
1414 TrollÄsen          | somebody else doing it wrong, without       |
NORWAY                  | comment.                                    |
                        |                                             |
Tlf:    +47 24 15 38 90 |                                             |
Fax:    +47 24 15 38 91 |                                             |
Mobile: +47 909  56 963 |                                             |
------------------------+---------------------------------------------+

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Mime
View raw message