August 28th, 2008

CICE ambiguity

Wherein I post a question where the CICE-ification would be unclear. CICE proponents - how should this situation be handled?

Mapping in java:

public interface MapFunction<R, T> {
	R function(T in);
}

public class Collections {
	public static <R, T> Collection<R> map(
	  Collection<T> list, MapFunction<R, T> f) {
		List<R> retvals = new LinkedList<R>;
		for ( T t : list ) retvals.add(f.function(t));
		return retvals;
	}
}

Now, given the above, let’s say I want to run Math.sin on a bunch of numbers:

Collection<Double> list = Arrays.asList(1.0, 2.0, 3.0, 4.0, 5.0);
Collection<Double> results = Collections.map(list, MapFunction(Double in) {
	return Math.sin(in);
});

The problem with the above fragment: Will the inference engine realize that the ‘R’ generics parameter of MapFunction should in this case bind to ‘Double’ as that’s the only thing we ever return here?

Should the inference engine do this automatically? (I don’t think it should)

Will it infer from the fact that we’re assigning to a Collections<Double>? Should the inference do this automatically? (Here, yes, I think so).

As a total aside, having map for java is fun, and CICE makes it relatively painless.

Leave a Response

(Note: if you use a new name from an unknown ip address, your comment won't appear until I approve it. Anti-spam measure only, I don't censor).

Imhotep theme designed by Chris Lin. Proudly powered by Wordpress.
XHTML | CSS | RSS | Comments RSS