fork download
  1. public class Main {
  2.  
  3. interface Foo { }
  4.  
  5. interface Bar { }
  6.  
  7. static class FooBarImpl implements Foo, Bar { }
  8.  
  9. //a simple container class
  10. static class FooBarContainer<T extends Foo & Bar> {
  11.  
  12. private final T fooBar;
  13.  
  14. public FooBarContainer(T fooBar) {
  15. this.fooBar = fooBar;
  16. }
  17.  
  18. public T get() {
  19. return fooBar;
  20. }
  21.  
  22. //a silly static unwrap method
  23. static <T extends Foo & Bar> T unwrap(FooBarContainer<T> fooBarContainer) {
  24. return fooBarContainer.get();
  25. }
  26. }
  27.  
  28. public static void main(String[] args) {
  29.  
  30. // concrete parameterized type
  31. FooBarContainer<FooBarImpl> fooBarContainer = new FooBarContainer<FooBarImpl>(new FooBarImpl());
  32. // wildcard parameterized type
  33. FooBarContainer<?> unknownFooBarContainer = fooBarContainer;
  34.  
  35. // passing in a concrete parameterized type
  36. FooBarContainer.<FooBarImpl>unwrap(fooBarContainer); // T is specified to be FooBarImpl
  37. FooBarContainer.unwrap(fooBarContainer); // T is inferred to be FooBarImpl
  38.  
  39. // passing in a wildcard parameterized type
  40. FooBarContainer.<?>unwrap(unknownFooBarContainer); // T is specified to be ? - compiler error
  41. FooBarContainer.unwrap(unknownFooBarContainer); // T is inferred to be ? - legal (!)
  42.  
  43. // unwrapping unknownFooBarContainer returns a reference of type ? extends Foo & Bar
  44. // we can assign to Foo or Bar, but not both
  45. Foo foo = FooBarContainer.unwrap(unknownFooBarContainer);
  46. Bar bar = FooBarContainer.unwrap(unknownFooBarContainer);
  47.  
  48. // if unwrap were expensive, we would be forced to cast
  49. Foo foo2 = FooBarContainer.unwrap(unknownFooBarContainer);
  50. Bar bar2 = (Bar)foo2;
  51.  
  52. // so this is where the hypothetical syntax would come in handy
  53. Foo&Bar fooBar = FooBarContainer.unwrap(unknownFooBarContainer);
  54. }
  55. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:40: error: illegal start of type
        FooBarContainer.<?>unwrap(unknownFooBarContainer);   // T is specified to be ? - compiler error
                         ^
Main.java:40: error: <identifier> expected
        FooBarContainer.<?>unwrap(unknownFooBarContainer);   // T is specified to be ? - compiler error
                          ^
Main.java:40: error: illegal start of expression
        FooBarContainer.<?>unwrap(unknownFooBarContainer);   // T is specified to be ? - compiler error
                           ^
Main.java:40: error: illegal start of expression
        FooBarContainer.<?>unwrap(unknownFooBarContainer);   // T is specified to be ? - compiler error
                                 ^
Main.java:40: error: ';' expected
        FooBarContainer.<?>unwrap(unknownFooBarContainer);   // T is specified to be ? - compiler error
                                                        ^
Main.java:53: error: not a statement
        Foo&Bar fooBar = FooBarContainer.unwrap(unknownFooBarContainer);
           ^
Main.java:53: error: ';' expected
        Foo&Bar fooBar = FooBarContainer.unwrap(unknownFooBarContainer);
               ^
7 errors
stdout
Standard output is empty