0

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?

eekboom
  • 5,551
  • 1
  • 30
  • 39
  • The documentation is about method resolution under TypeChecked or CompileStatic operation isn't it, not type coercion? – tim_yates Jan 05 '21 at 22:36
  • @tim_yates Oh yes, on second look that section applies to TypeChecked I think (but not to CompileStatic). However, that still leaves the question open: Where is the type coercion to BigDecimal documented? – eekboom Jan 07 '21 at 08:08

0 Answers0