[ 
https://issues.apache.org/jira/browse/GROOVY-6324?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17881003#comment-17881003
 ] 

Eric Milles commented on GROOVY-6324:
-------------------------------------

Proper static compilation support would require a transform from the call expr 
"a.b(c)" to "a.b.call(c)". This would require checking for a callable property 
"a.b" in StaticTypeCheckingVisitor visitMethodCallExpression (likely just 
before handleMissingMethod) and then some conversion in 
MethodCallExpressionTransformer similar to how call on a closure variable is 
handled. This is modeled after the dynamic MetaClassImpl's 
invokePropertyOrMissing.

Without something like that, it is possible to allow the dynamic handling of 
callable property via a type-checking extension. This should be possible for 
any release since Groovy 2.1:
{code:groovy}
class Car {
  Closure model
}

@CompileStatic(extensions="scripts/Checker.groovy")
void test() {
  def car = new Car(model: { -> 'Tesla' })
  print car.model()
}

test()

// Checker.groovy ::
methodNotFound { rcvr, name, args, types, call ->
  if (call instanceof MethodCallExpression) {
    def property = new PropertyExpression(call.getReceiver(), name)
    property.setImplicitThis(call.isImplicitThis())
    if (existsProperty(property, true)) {
      def type = getType(property)
      if (type.equals(CLOSURE_TYPE)) {
        if (type.getGenericsTypes()) {
          type = type.getGenericsTypes()[0].getType()
        } else {
          type = OBJECT_TYPE
        }
        return makeDynamic(call, type)
      }
    }
  }
}
{code}

> Cannot call closure like a method in type checked mode
> ------------------------------------------------------
>
>                 Key: GROOVY-6324
>                 URL: https://issues.apache.org/jira/browse/GROOVY-6324
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>    Affects Versions: 2.2.0-beta-1, 2.3.0, 2.4.0-rc-1
>            Reporter: Peter Niederwieser
>            Priority: Major
>
> {{Closure closure = ...; closure()}} doesn't work in type-checked mode. 
> Instead, one has to use {{closure.call()}}.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to