Richard Searle's Blog

Thoughts about software

Attempting to resolve the XPathFunction and Scala int problem

Posted by eggsearle on September 9, 2010

As previously noted, there are difficulties returning a Scala “primitive” type.

A view might provide a way to resolve that problem

class O[T,R<%Object](val f:Function1[T,R])  extends XPathFunction {  def evaluate(list:java.util.List[_]) = f(list.get(0).asInstanceOf[T])}
implicit def f1ToO[T,R<%Object](f:Function1[T,R]):O[T,R]=new O(f)
Map[String,XPathFunction]("a1"->{i:Int=>i*2})

Note the explicit return type on the f1ToO function, which is needed to avoid an error indicating that recursive functions need a return type!

Unfortunately, this approach fails because there are two implicit conversions in Predef.

This problem can be resolved by explicitly specifying the desired final type

class OInt[T,R<%java.lang.Integer](val f:Function1[T,R])  extends XPathFunction {  def evaluate(list:java.util.List[_]) = f(list.get(0).asInstanceOf[T])}
implicit def f1ToOInt[T,R<%java.lang.Integer](f:Function1[T,R]):OInt[T,R]=new OInt(f)

val mint= Map[String,XPathFunction]("int"->{i:Int=>i*2})
mint("int").evaluate(Collections.singletonList(12))

It is fortunate that only a few such mappings are needed to cover the impacted Scala types

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: