1

I'm trying to make String/GString objects callable with something like

String.metaClass.call = { println toUpperCase() + " " + it() }

This is producing a MissingMethodException when called on literals, though not always:

def a = 'abc'

a{ 2 * 3 }     //ABC 6                     -> OK
'a'{ 2 * 4 }   //ABC 8                     -> OK
'abc'{ 2 * 5 } //MissingMethodException    -> ERROR

The error is:

groovy.lang.MissingMethodException: No signature of method: ConsoleScript11.abc() 
is applicable for argument types: (ConsoleScript11$_run_closure4) 

I'll skip the fact that 'a'{ 2 * 4 } works while 'abc'{ 2 * 5 } doesn't. My question really is: is there a way to make invocations on literals work without Groovy trying to resolve methods with quoted identifiers?

ernest_k
  • 44,416
  • 5
  • 53
  • 99
  • 1
    `'abc'.call{ 3 }` works just fine, if it can save you – injecteer Nov 13 '20 at 16:17
  • @injecteer Yes. But I really want to use the call operator on literals as a user feature in a dsl if that can be systematically achieved – ernest_k Nov 13 '20 at 16:38
  • btw I think, if you put your stuff in a proper class (not a Script), then groovy won't apply part of it's magic, and `No signature of method: ConsoleScript11.abc()` wouldn't be thrown – injecteer Nov 14 '20 at 11:40

0 Answers0