Menu

Auxiliary Function with no return type (void vs. boolean)

2013-07-16
2013-07-16
  • Jens von Pilgrim

    If an auxiliary function is declared with no return type, boolean is assumed as the return type. Is it possible to declare an auxiliary function returning nothing (i.e. void)? Actually it is possible to declare and define the function as follows

    auxiliary {
        foo(PType p): void          
    }
    auxiliary foo(PType p) {}
    

    But this leads to an error in the generated code:

    private PolymorphicDispatcher<void> fooDispatcher;
    ...
    

    Well, as a workaround I could define the auxiliary function in an external Java or Xtend class (in case of Xtend, I could even use multi dispatch). Actually, that makes me wonder why I should define auxiliary functions in the first place? Are there any interesting members which can only be accessed via the auxiliary function (and not from an external method)?

    Regards,
    Jens

     
  • Jens von Pilgrim

    One addition: The generated Java implementation catches exceptions and returns null in that case. This only works for declared types (Objects etc), and not for primitive types. E.g., the generated code for an auxiliary function

    auxiliary {
        foo(String s): boolean      
    } 
    
    auxiliary foo(String s) {
        false;
    }   
    

    contains compilation errors:

    protected boolean fooImpl(final RuleApplicationTrace _trace_, final String s) throws RuleFailedException {
        try {
          ..
          return _result_;
        } catch (Exception e_applyAuxFunFoo) {
          ..
          return null; <------- ERROR
        }
      }
    

    As a workaround, one can declare Boolean instead of boolean.

    Another addition:

    auxiliary {
        bar(): Boolean      
    } 
    
    auxiliary bar() {
        false;
    }   
    

    will result in another non-compiling code, as the generator template expects at least one parameter:

    public Boolean foo() throws RuleFailedException {
        return foo(null, );
    }
    

    Regards,
    Jens

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.