News for hackers. Stuff that matters.
Re:Type erasure and reification by Matthias247 on Wednesday December 05, 2018 @01:54PM (#18611253)
Besides those you also can't do something like

    Object xyz = ...;
    if (xyz instanceof List<String>) { doA(); }
    else if (xyz instanceof List<Integer> { doB(); }
because there is only a single List type at runtime.
Re:Type erasure and reification by bunderbunder on Wednesday December 05, 2018 @03:18PM (#18612078)
As long as you are able to take the same code path for all empty lists, you can do something like

  if (xyz instanceof List) {
    if (xyz.isEmpty()) { ... }
    else if (xyz.get(0) instanceof String) { ... }
    else if (xyz.get(0) instanceof Integer) { ... }
  }
Which, I admit, is a gross hack.
Re:Type erasure and reification by int_19h on Wednesday December 05, 2018 @05:10PM (#18613162)
And, conversely, you can't implement both List<String> and List<Integer> on the same class.
Re:Type erasure and reification by bunderbunder on Wednesday December 05, 2018 @06:01PM (#18613636)
Though, even in C#, it would be inadvisable. If you have the same class implement both `IList<String>` and `IList<int>` you'll have some ambiguity to deal with in implementing

  object IList.this[int index] { ... }
The expectation is that that property's getter should just upcast the return value of the generic version of the indexer.

For both languages, you'd be better off having two separate methods for the two different kinds of list.

Re:Type erasure and reification by int_19h on Wednesday December 05, 2018 @06:20PM (#18613775)
The point of doing this is not to invoke these members directly on the object, though. It's to allow the object to be passed to anything that expects either IList<string> or IList<int>, or for runtime discovery purposes with is/as.

Perhaps a better example is something like IConvertibleTo<T>. There's an obvious meaning to a class implementing it for many different Ts.