java - Why it is not possible to place type parameters after method name? -
if fact, question is:
what problems may occure if possible place type parameters after method name?
e.g.:
import com.google.common.base.predicate; static import com.google.common.base.predicates.alwaystrue; predicate<sometype> p = alwaystrue<sometype>();
update:
i know pass type parameter way:
predicate<sometype> p = predicates.<sometype>alwaystrue();
i see 2 distinct questions here:
why class or object needed parametrize method?
public class test { private static <t> void staticmethod() {} private <t> void method() {} public test() { method(); // works! <integer>method(); // doesn't work! this.<integer>method(); // works again! } public static void main(string[] args) { staticmethod(); // works! <integer>staticmethod(); // doesn't work test.<integer>staticmethod(); // works again! } }
unfortunately cannot give answer that...
second question: why method type parameters put before method (as opposed class parameters, after class)?
here have hunch - 1 of problems generic constructors. generic constructors can have type parameters other method, , can independent of class type parameters:
public class class<t> { public <v>class(v arg) { } public static void main(string[] args) { new <number>class<object>(0); } }
this valid syntax, , first type parameter method parameter, while second 1 class type parameter.
there needs mechanism distinguish between those, , remember - either can omitted on developers whim, because of backwards compatibility!
i cannot think of other way it, rather putting them on different sides of constructor call.
i imagine there similar fringe case of ambiguity parametrized method being called without object or class, can't think of right now.
Comments
Post a Comment