Question on exception transparency

Maurizio Cimadamore maurizio.cimadamore at oracle.com
Thu Aug 12 05:52:09 PDT 2010


On 12/08/10 13:40, Peter Levart wrote:
> import java.util.*;
>
> public class Closures
> {
>    public interface Block<T, throws E>  { void run(T param) throws E; }
>
>    public static<T, throws E>
>      void forEach(Iterable<T>  iterable, Block<? super T, E>  block) throws E
>    {
>      for (T obj : iterable) block.run(obj);
>    }
>
>    public static void main(String[] args)
>    {
>      // example 1:
>      forEach(
>        Arrays.asList("a", "b", "c"),
>        #(String s){ System.out.println("element: " + s); }
>      );
>
> // example 2 - javac throws NPE:
> //    Closures.<String, void>forEach(
> //      Arrays.<String>asList("a", "b", "c"),
> //      Block<String, void>  #(String s){ System.out.println("element: " + s); }
> //    );
>    }
> }
>    
The following works for me:

Closures.forEach(
       Arrays.<String>asList("a", "b", "c"),
       Block<String, void> #(s){ System.out.println("element: " + s); }
     );

I guess  the NPE has been fixed in the last push.

However I noticed a problem when 'void' is specified as explicit 
type-parameter for a generic method - for example, the following doesn't 
work:

Closures.<String, void>forEach(
       Arrays.<String>asList("a", "b", "c"), null);

I will investigate on this...

Maurizio


More information about the lambda-dev mailing list