Richard Searle's Blog

Thoughts about software

The magic of Scala type inferencing

Posted by eggsearle on September 6, 2010

Considering mapping the Java XPathFunction into a standard Scala function represention.

An implemention for a single parameter might be

abstract class One[T,R<:Object] extends Function1[T,R] with XPathFunction { 
      def evaluate(list:java.util.List[_]) = apply(list.get(0).asInstanceOf[T]) 

With the identity function defined as

object OneString extends One[String,String]{ 
    def apply(s:String)=s

The repl has the following result

scala> OneString.evaluate(Collections.singletonList("ddd"))                                                                                                  
res22: String = ddd
scala> val xf:XPathFunction = OneString
xf: javax.xml.xpath.XPathFunction = <function1>
scala> xf.evaluate(Collections.singletonList("ddd"))       
res24: java.lang.Object = ddd

Note the types of the return values! The same function is being executed in each case, but via two aliases which have different associated types.


Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s

%d bloggers like this: