Shevek (shevek) wrote,
Shevek
shevek

There is a type pattern missing from (both C++ and?[0]) Java, to do with type parameterization: The simplest example is an InstanceMap, that is, a Map<Class<?>, ?> with a method public <T> T get(Class<T> type). I'm sure I've seen this somewhere as custom code (but I can't immediately see in commons-collections or guava[1]). The trouble is that it's impossible to write type parameters to Map<?,?> which generate a get() method which has the type dependency between the argument and the return value. I'm not even sure it's possible in C++ using any sort of generic map interface - it has to be a non-standard-interface method.

The more complex case is (again, referring to commons-collections-15) a type I find in our code: Map<Class<?>,Transformer<? extends Object,String> - the intent is to provide a type-dispatch of (in this case) a toString() method, which was not necessarily provided by the original developer. Again, what one wants is the dependency between the argument and the returned value, that is: public <T> Transformer<T,String> get(Class<T> type).

The challenge is even to write a generic Map<?,?>-like interface such that by giving appropriate type parameters, the general class of such interdependencies can be expressed. Certainly, any particular case of interdependency can be written up as an explicit function. But can it be written in any popular language as a generic type, for example, by doing something with manipulating captured types in syntax? I'm interested in solutions in any language which permits the compiler to show (within reason) the correctness of the code.

[0] The fact that it can (kind of) be done in the C preprocessor suggests that it might be possible in C++, but I'm not the expert.
[1] Somebody please tell the world why Google reinvented that particular wheel?

Edit: Solutions in typed lambda calculus are fine. I haven't got that head on at the moment. Please explain whether or not this is a facility which is generally missing from the languages in question.
Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 7 comments