Hi Eric,
I am using STC a lot i n my framework, and have run into problems with
"too much strictness". My take would be:
m1 : STC error - giving a floating point literal when an integer is
expected could point towards an error, and there is no inconvenience in
writing 1 instead of 1.0 .
m2: Should definitely work - one of the strengths of Groovy is not being
required to write the annoying/ugly 1.0f / 1.0d explicit double/float
literals.
m3: STC error - without any doubt, since even dynamic Groovy rightfully
rejects that, and it is imho generally asking for trouble to do
autoconversions like that.
As a guideline I would suggest to make it as convenient as possible, and
generally what most people would expect to work without a hassle - as
long as it does not violate least surprise, or go into the area of
potentially introducing hard to track/spot errors.
Cheers,
mg
PS: I have not checked, but for completeness I would also expect
m2(1) // integer literal given
to work.
On 21/11/2019 17:05, Milles, Eric (TR Tech, Content & Ops) wrote:
I wanted to discuss the strictness of Groovy's static type checking
before Groovy 3 is released and change becomes difficult. Recent
changes have replaced some instances of type coercion with STC
errors. It was suggested in one of the bug tickets to discuss before
accepting the new strictness or making some adjustments to allow some
number type conversions and possibly allow toString() conversions.
Each of the three method call expressions below produce this error:
"[Static type checking] - Cannot find matching method
bugs.Groovy8488#m1(java.math.BigDecimal). Please check if the declared
type is correct and if the method exists."
*Do we expect number literals (such as BigDecimal, BigInteger or even
just Double) to be acceptable arguments to methods that accept "int",
"double" or "String"? Is there some limited set of automatic
conversions that STC should support?* Dynamic Groovy supports the m1
and m2 calls, but throws MissingMethodException for m3.
@groovy.transform.CompileStatic
class STC {
void m1(int i) {}
void m2(double d) {}
void m3(String str) {}
void test() {
m1(1.0)
m2(1.0)
m3(1.0)
}
}
https://issues.apache.org/jira/browse/GROOVY-8488
https://issues.apache.org/jira/browse/GROOVY-9224