0

I would expect in the code below :

public static <K, D extends List<T>, T> List<Map<K, D>> meth(K k, D d) {
    List<Map<K, D>> daBytes = (List<Map<K, D>>)
            new ArrayList<Map<K, List<List<Byte>>>>();
    // ...
}

the compiler to figure out that D is a List<List<Byte>> and/or that T is a <List<Byte> - and to actually get rid of the cast. Instead I get :

Cannot cast from ArrayList<Map<K,List<List<Byte>>>> to List<Map<K,D>>

and I need the cast anyway.
Why ? Is what I want somehow possible (without the strategy pattern workaround) ?

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361

1 Answers1

2

D is specified by the caller of meth; you can't force it to be some other particular type inside meth. If by D you mean List<List<Byte>>, then you should write that:

List<Map<K, List<List<Byte>>>> daBytes = new ArrayList<Map<K, List<List<Byte>>>>();

If you mean for D to be arbitrary, then you should be writing

List<Map<K, D>> daBytes = new ArrayList<Map<K, D>>();
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • I see - `D` can either be a `List>` or a `List` - see code [here](http://stackoverflow.com/q/18749426/281545). So I know it must be a list of something - `?` is no use as it can't be manipulated and anyway I need to assign it to a specific type. Anyway I can do what I want (question 2) ? – Mr_and_Mrs_D Sep 17 '13 at 20:01
  • No, you can't really do what you're trying to do like that. You could do `ArrayList>`, though, but you couldn't restrict `T` to be either `Byte` or `List`. – Louis Wasserman Sep 17 '13 at 20:03
  • Apparently I wasn't the only one to think about this functionality: https://github.com/functionaljava/functionaljava/blob/master/core/src/main/java/fj/data/Either.java – Mr_and_Mrs_D Jun 07 '14 at 18:31