Skip to content
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

Static type checking fails in Greclipse within closure (closure declared argument types ignored) #1327

Closed
mauromol opened this issue Dec 30, 2021 · 2 comments
Assignees
Labels
Milestone

Comments

@mauromol
Copy link

Using Greclipse 4.4.0.v202112282355-e2009.

Consider this Java utility class:

package test70;
import java.util.List;
import java.util.function.BiConsumer;

public class Util {
	public static <T> BiConsumer<String, ? super List<T>> batchFromIndividualPostProcessor(
			final BiConsumer<String, ? super T> postProcessor) {
		return (text, batch) -> {
			for (final T element : batch)
				postProcessor.accept(text, element);
		};
	}
}

and this Groovy class:

package test70
import groovy.transform.CompileStatic

@CompileStatic
class Test70 {
	void foo() {
		Util.batchFromIndividualPostProcessor { String text, Number number ->
			number.toBigDecimal()
		}
	}
}

A compilation error is produced on number.toBigDecimal() because number is detected as Object rather than Number.

Please note that:

  • this was compiling fine with some versions of Greclipse ago (cannot say exactly which one now)
  • this compiles fine with Gradle/groovyc
  • switching to @CompileDynamic makes things work

I have other more complex use cases in my real-world project which I think are related to this same problem. It seems like Greclipse is simply ignoring now the type I'm declaring for closure arguments.

@mauromol mauromol changed the title Static type checking fails in Greclipse within closure Static type checking fails in Greclipse within closure (closure declared argument types ignored) Dec 30, 2021
@eric-milles
Copy link
Member

eric-milles commented Dec 30, 2021

I have one fix that will allow you to put in the type arguments explicitly, like:

		Util.<Number>batchFromIndividualPostProcessor { string, number ->
			number.toBigDecimal()
		}

I am still sorting out the parameter type recognition. STC wants to compare the declared types to the inferred types at some point so it can create errors for incompatible types.

@eric-milles
Copy link
Member

This will be an issue in Groovy 4, so I have created a ticket to track: https://issues.apache.org/jira/browse/GROOVY-10436

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants