J
Jon Harrop
I am trying to write some software in Java and, amongst other problems, I'm
finding it very difficult to express simple concepts in Java.
For example, when writing RPC bindings I would like to write conversion
functions that translate a Java Set<T> into a format for the wire and back
again, where T is some type.
Ordinarily I would just pass the conversion function for the type T to the
conversion function for a polymorphic set but Java doesn't seem to support
higher-order functions. What hoops do I have to jump through to get this
done? I've considered wrapping every conversion function in a class in
order to mimic the passing of functions (as the passing of 1-method objects
instead). I've also considered using immediate objects to wrap functions at
each call site. Neither of these approaches seem reasonable, e.g. they are
both hugely verbose.
I also have some irritations regarding the stdlib. I had heard that it was
comprehensive but it appears to be largely superfluous. For example, I
believe the following is minimal Java code to load and save some XML:
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
public class dom {
public static void main(String[] argv)
throws javax.xml.transform.TransformerConfigurationException,
javax.xml.parsers.ParserConfigurationException,
javax.xml.transform.TransformerException,
java.io.FileNotFoundException,
java.io.IOException
{
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DOMImplementation i = (f.newDocumentBuilder()).getDOMImplementation();
Document doc = i.createDocument(null, null, null);
InputStream in = new FileInputStream("test.xml");
t.transform(new StreamSource(in), new DOMResult(doc));
in.close();
Transformer t = (TransformerFactory.newInstance()).newTransformer();
t.transform(new DOMSource(doc), new StreamResult(System.out));
}
}
Are these patterns not factored out into a library function somewhere? If
not, why not? Finally, why does that Java not work?
Here's the OCaml for comparison:
Xml.to_string (Xml.parse_file "test.xml")
Cheers,
Jon.
finding it very difficult to express simple concepts in Java.
For example, when writing RPC bindings I would like to write conversion
functions that translate a Java Set<T> into a format for the wire and back
again, where T is some type.
Ordinarily I would just pass the conversion function for the type T to the
conversion function for a polymorphic set but Java doesn't seem to support
higher-order functions. What hoops do I have to jump through to get this
done? I've considered wrapping every conversion function in a class in
order to mimic the passing of functions (as the passing of 1-method objects
instead). I've also considered using immediate objects to wrap functions at
each call site. Neither of these approaches seem reasonable, e.g. they are
both hugely verbose.
I also have some irritations regarding the stdlib. I had heard that it was
comprehensive but it appears to be largely superfluous. For example, I
believe the following is minimal Java code to load and save some XML:
import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import org.w3c.dom.*;
public class dom {
public static void main(String[] argv)
throws javax.xml.transform.TransformerConfigurationException,
javax.xml.parsers.ParserConfigurationException,
javax.xml.transform.TransformerException,
java.io.FileNotFoundException,
java.io.IOException
{
DocumentBuilderFactory f = DocumentBuilderFactory.newInstance();
DOMImplementation i = (f.newDocumentBuilder()).getDOMImplementation();
Document doc = i.createDocument(null, null, null);
InputStream in = new FileInputStream("test.xml");
t.transform(new StreamSource(in), new DOMResult(doc));
in.close();
Transformer t = (TransformerFactory.newInstance()).newTransformer();
t.transform(new DOMSource(doc), new StreamResult(System.out));
}
}
Are these patterns not factored out into a library function somewhere? If
not, why not? Finally, why does that Java not work?
Here's the OCaml for comparison:
Xml.to_string (Xml.parse_file "test.xml")
Cheers,
Jon.