javaannotationsif-statementreturn-valuejml

If statement with return in JML


i need to set up a postcondition which ensures to return null if size_ is 0. Based on

 if(size_ == 0)
  return null;

how can i do that in jml? any ideas? Following doesn't work:

//@ ensures size_ == null ==> \return true;

thanks in advance


Solution

  • Try

    //@ ensures size_ == null ==> \result == true;
    

    Example:

    //@ ensures size_ == null ==> \result == true;
    public boolean sizeUndefined() {
        if (size_ == null)
            return true;
    
        return size_.length() > 0;
    }
    

    You could also simply write it like this:

    //@ ensures size_ == null ==> \result;
    

    Here is the documentation for \result:

    3.2.14 \result
    Within a normal postcondition or a modification target of a non-void method, the special identifier \result is a specification expression whose type is the return type of the method. It denotes the value returned by the method. \result is allowed only within an ensures, also_ensures, modifies, or also_modifies pragma that modifies the declaration of a non-void method.