When I have a Java method that takes a BigDecimal parameter:
public void setAmount(BigDecimal amount)
then I can call it from Groovy using an integer argument like
m.setAmount(11)
Can anybody point me to Groovy documentation where this type coercion (?) is defined?
Extract from https://docs.groovy-lang.org/docs/next/html/documentation/core-semantics.html#_method_resolution
An argument o of type A can be used for a parameter of type T if and only if:
...
- or T and A derive from java.lang.Number and conform to the same rules as assignment of numbers
When I then look at the referenced rules for "assignment of numbers", there is no mention of T being BigDecimal: https://docs.groovy-lang.org/docs/next/html/documentation/core-semantics.html#number-assignment
In this question Converting Integer to BigDecimal in Groovy the answer gives a reference to Groovy's BigDecimalCachedClasscoerceArgument(Object argument). That method now even has a simpler implementation:
public Object coerceArgument(Object argument) {
if (argument instanceof Number) {
return NumberMath.toBigDecimal((Number) argument);
}
return argument;
}
So: Is the documentation just outdated?