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

Groovy named parameters static check

    XMLWordPrintableJSON

Details

    Description

      It would be really nice if groovy will include some annotation for type checking named params.
      For example we do not know what map is expected until we go into source.

      Annotation
      @Retention(RetentionPolicy.RUNTIME)
      @Target({ElementType.PARAMETER})
      public @interface NamedParams {
      
          NamedParam[] value();
          boolean unknowns() default false;
      
          @interface NamedParam {
              String value();
              Class type() default Object.class;
              int genericTypeIndex() default -1;
              String target() default "";
              boolean required() default false;
          }
      }
      
      Example Usage
      def foo(@NamedParams([
              @NamedParam("bar"),
              @NamedParam(value = "baz", type = String)]) Map map) {
          println map.bar
          println map.baz
      }
      
      foo(bar: [], baz: "")
      

      In @CompileStatic case it could be used to fail the compilation if user tries to get some unknown value from map, and also fail if user does not pass some required argument.

      Also it could be used to annotate map-constructors in compile-time.

      And the last one: it will be VERY useful for IDEs.

      Attachments

        Issue Links

          Activity

            People

              paulk Paul King
              daniilo Daniil Ovchinnikov
              Votes:
              8 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: