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

@Memoized AST Transformation for Methods

    XMLWordPrintableJSON

Details

    • New Feature
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 2.2.0-beta-1
    • None

    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

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

            Dates

              Created:
              Updated:
              Resolved: