-
-
Notifications
You must be signed in to change notification settings - Fork 314
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generics type inference minor issue #239
Comments
Interesting. It makes sense since in Java the left-hand side it always typed. The same problem will occur in Java 10 with Hum... I don't really have a solution for that. Maybe I was wrong to change the typing and I should add a |
I think because of type erasure of java the only way to do this properly is with concrete TypeRef anonymous class instances as per solution 1 in the linked issue. I'm not a generics expert, but that's how I've always done, for example, binding a generic in a guice module (using their TypeLiteral class). |
#229 also made the situation much worse for IDE tooling: Consider:
Then do, e.g in Eclipse, QuickFix -> Assign statement to new local variable.
instead of
This applies to other tooling support such as Create method, Extract method, etc. It's very easy to create methods / variables with wrong types now. I think relaxing the typing with #229 has much more drawbacks than benefits. In the situation with
is not that big of a deal, IMHO. |
fwiw, we've got a class that provides some inline functions to make some of this nicer: https://github.com/trib3/leakycauldron/blob/master/testing/src/main/kotlin/com/trib3/testing/LeakyMock.kt the balance between using that class and using EasyMock directly isn't the cleanest thing in the world, but if it's helpful to anyone, it's available. |
@josephlbarnett How do it works? The reify version can receive |
with the reified version, you can do |
I think I face the same issue that causes a While I still don't understand the different behavior of compiler and runtime here, the following works in 3.4 and fails in 4.0.2.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>example.app</artifactId>
<version>1.0-SNAPSHOT</version>
<name>example.app</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<!-- code works -->
<!-- <version>3.4</version> -->
<!-- code fails -->
<version>4.0.2</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
package com.example;
import static org.junit.Assert.assertTrue;
import javax.swing.tree.TreePath;
import org.junit.Test;
import org.easymock.EasyMock;
public class AppTest
{
@Test
public void shouldCompileAndRun()
{
TreePath path = new TreePath( EasyMock.createNiceMock( Runnable.class ));
}
} I have a similar (but written in Java) approach like @josephibarnett. This works for me, but what's your suggestion, to change the test code or to improve the generic code in EasyMock? |
I'm biaised. I like very much the relaxed typing but I do agree it causes problems. I'm in front of
So as you can see, with the relaxed way, everything can work without warning. It just requires a type witness. But I agree that for cases where the inference used to work, it's annoying. |
I've been using easymock 3.4 from Kotlin tests, where the mock can be declared like:
After upgrading to easymock 4.0.2 (Likely related to #229), that fails to compile due to type inference problems, and must be replaced with either:
or:
Overall a pretty minor thing, but not having to repeat the for non generics was nice.
The text was updated successfully, but these errors were encountered: