3

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

trnc
  • 20,581
  • 21
  • 60
  • 98

2 Answers2

4

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.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • Thats what I am saying. It sounded to me like OP is trying to replace the `if-else` thingie, therefore I said that. I hope there is no confusion anymore. – Adeel Ansari Dec 06 '10 at 09:24
  • Oh, I see. I doubt the OP believes he can replace code with "special comments" though. – aioobe Dec 06 '10 at 09:25
  • The confusion came from the snippet presented by OP. If he/she has shown the method with JML comments, everything would have been fine. – Adeel Ansari Dec 06 '10 at 09:27
1

First of all: what type is size_, Object or primitive(int)?

Secondly, what the return type of the method? Object or primitive(boolean)?

You cannot compare a primitive type to null, or return null where a primitive type is supposed to be returned. If we assume size_ is int and the return is boolean then the the post-condition would be

//@ ensures size_ == 0 ==> \result;
Geoff Reedy
  • 34,891
  • 3
  • 56
  • 79