Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-4993

@Memoized AST Transformation for Methods

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.2.0-beta-1
    • Component/s: None
    • Labels:

      Description

      The whole idea is similar to existing great @Lazy annotation, but it differs in concept: instead of being applied to fields it is applied to methods, thus providing a wider field of use. When applied to getters it serves as an alternative to @Lazy, but applied to other methods it provides what @Lazy can't. Thus it eliminates the need for heavy refactoring in certain situations, by simply letting the user add the annotation to the method.

      Here is a suggestion of how it could work:

      @Cached
      T createX() {
        new T(1, 2, 3)
      }   
      

      gets transformed into:

      private T $createX$result
      T $createX() {
        new T(1, 2, 3)
      }
      T createX() {
        T $result_local = $createX$result
        if ($result_local != null)
          return $result_local
        else {
          synchronized (this) {
            if ($createX$result == null) {
              $createX$result = $createX()
            }
            return $createX$result
          }
        }
      }
      

      This whole thing could be extended to cache different results of a method depending on its arguments, but it's a topic for a discussion.

        Attachments

          Activity

            People

            • Assignee:
              paulk Paul King
              Reporter:
              mojojojo Nikita Y Volkov
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: