painless-method-dispatch.asciidoc 1.7 KB

123456789101112131415161718192021222324252627282930
  1. [[modules-scripting-painless-dispatch]]
  2. === How painless dispatches functions
  3. Painless uses receiver, name, and https://en.wikipedia.org/wiki/Arity[arity]
  4. for method dispatch. For example, `s.foo(a, b)` is resolved by first getting
  5. the class of `s` and then looking up the method `foo` with two parameters. This
  6. is different from Groovy which uses the
  7. https://en.wikipedia.org/wiki/Multiple_dispatch[runtime types] of the
  8. parameters and Java which uses the compile time types of the parameters.
  9. The consequence of this that Painless doesn't support overloaded methods like
  10. Java, leading to some trouble when it whitelists classes from the Java
  11. standard library. For example, in Java and Groovy, `Matcher` has two methods:
  12. `group(int)` and `group(String)`. Painless can't whitelist both of these methods
  13. because they have the same name and the same number of parameters. So instead it
  14. has `group(int)` and `namedGroup(String)`.
  15. We have a few justifications for this different way of dispatching methods:
  16. 1. It makes operating on `def` types simpler and, presumably, faster. Using
  17. receiver, name, and arity means that when Painless sees a call on a `def` object it
  18. can dispatch the appropriate method without having to do expensive comparisons
  19. of the types of the parameters. The same is true for invocations with `def`
  20. typed parameters.
  21. 2. It keeps things consistent. It would be genuinely weird for Painless to
  22. behave like Groovy if any `def` typed parameters were involved and Java
  23. otherwise. It'd be slow for it to behave like Groovy all the time.
  24. 3. It keeps Painless maintainable. Adding the Java or Groovy like method
  25. dispatch *feels* like it'd add a ton of complexity which'd make maintenance and
  26. other improvements much more difficult.