Java supports generic types and methods. Why not generic fields? -


Despite the fact that janetics [Java Runtime] is not in Java, I was recently caught in some kind of generic Hell is there

Status (simplified and useless reference removed):

A library is A, which provides interface IA. A library is B, which provides interface IB. Both libraries are third party libraries on which I do not have any control. (In reality, there are more than 2 but they are not so complex yet.)

An engine developed by my company , Which defines both objects and heavily uses / implementing both IA and IB Since our engine should deal with all implementations, we should only implement some interface IAB, IB Can not define and allow all sections to be implemented. Therefore, to get the collection time, type-security & amp; Method risk, it was decided to use normal methods. Therefore, whenever an 'IAB' is required, we will be able to & lt; IAB IA & amp; IB & gt; Do this with zero (IAB some object withboth API) .

It works very well, though in fact as a fact, & lt; It is not possible to do something simple as IAB IA IA & amp; IB & gt; IAB FieldwithbothAPIS; (Neither the the IA and the IB fields with the APIs ). Which tells us about the question:

Why not?

Actually, (design-level?) Arbitrary typeparams (opt) , while example field concrete type ?

JLS §8.3

  fieldDocumentation: Field Modifiers (opt) type variableDeveloper;   

JLS §8.4 )

A common method receives logic from its charge context on runtime, thus it can obtain different types in different circumstances.

How will a common field type argument get? In some contexts, there can be no different type of field depending on its usage, it should be a single type, which is known on time.


Comments

Popular posts from this blog

java - org.apache.http.ProtocolException: Target host is not specified -

java - Gradle dependencies: compile project by relative path -

ruby on rails - Object doesn't support #inspect when used with .include -