Name Collector Name

Consider:

public interface Foo<T> { public static class X{} public void foobar(T t); } public class Bar<X> { Foo<X> foo = new Foo<X>() { public void foobar(X t) {} }; } 

I did not find a way to express what I mean X from Bar<X> , not Foo.X in the foobar(X t) implementation. There is no other way but to rename the general parameter X to Bar or a static inner class?

+6
source share
2 answers

I donโ€™t think there is a way to unambiguously change the type parameter, and I think it was a reasonable design decision.

  • The conventions are clear that type parameters should be one character long, if possible, and the flip side of this is that other classes should not have single-character names.
  • If you had the opportunity to disambiguate, then you will have the opportunity to rename the type parameter X to Bar<X> . In other words, if you had the opportunity to say foobar(TypeParameter.X t) , you would have the opportunity to simply use something other than X for the type parameter on Bar . Renaming X is how you avoid name conflicts.

Remember that type parameter names do not extend to other classes in a more than trivial way. You should never use a parameter name of a particular type, ever. Therefore, it makes sense that language developers would not think that it is worth adding complexity to the language.

+9
source

the compiler does not even bother to determine if you mean Foo.X, it will consider TypeParameter X, regardless of the fact that you typed something like:

 public class Bar<X> { Foo<X> foo = new Foo<X>() { public void foobar(Foo.X t) {} }; } 
0
source

Source: https://habr.com/ru/post/906554/


All Articles