When I use deepEquals of two empty list it return true even that two lists are of different types. I know that the lists only is of different types while compiling and not runtime.
But is there opportunity of annotates the field or something familiar?
So this what I want:
List<Foo> fooList = []
List<Boo> booList = []
DeepEquals.deepEquals(fooList, booList) //I want it to return false, as the list differs on type
Doing runtime the list appears as ArrayList without typing
There is no solution for this right now in Java or Groovy. You are seeing Java type erasure at work. It was a decision made for things to work that way when generics were added to Java back with Java 1.5.
Basically the compiled class is List.
And each field is an instance of that type and are effectively both List<Object>
at runtime.
The information abut the parameter type is simply not available any longer.
A fuller explanation of type erasure can be found here:
Java generics type erasure: when and what happens?
Other runtimes such as .NET do have reified types, meaning List<Foo>
and List<Bar>
are two separate types. Both approaches have their pros and cons.
There has been some discussion and work for the JVM to have something similar in the future. Some of the talks at the JVM Language Summit (JVMLS) show that John Rose and others have been considering template classes and type species to try and solve this problem. So List would be the class but there could be a species
for each of List<Foo>
and List<Bar>
. But AFAIK that is several years away, if it ever arrives.