0

How to use the old keyword into ensure clause of a feature, the current statement doesn't seem to be valid at runtime

relationships: CHAIN -- any chain

some_feature
    do
    (...)
    ensure
        relationship_added: attached relationships as l_rel implies
            attached old relationships as l_old_rel and then
            l_rel.count = l_old_rel.count
    end

For the whole case, following screenshots demonstrate the case

start of routine execution enter image description here end of routine execution enter image description here

Pipo
  • 4,653
  • 38
  • 47
  • What do you mean by "doesn't seem to be valid"? What version of the compiler do you use? (The code compiles fine by EiffelStudio 18.11.) – Alexander Kogtenkov Jan 13 '19 at 18:51
  • I also have this version, it compiles but doesn't validate on runtime, with this semantic how would you write it? – Pipo Jan 13 '19 at 19:20
  • Would you describe what you expect and what you get, please? – Alexander Kogtenkov Jan 13 '19 at 19:37
  • Thx again for your help, I expect to create a statement which makes me able to validate that relationships has grown from 1 on ensure. The statement as written above is False even if it has grown ... – Pipo Jan 13 '19 at 19:40
  • @AlexanderKogtenkov added some screenshots, hope it helps – Pipo Jan 13 '19 at 19:46

1 Answers1

1

There are 2 cases to consider: when the original reference is void, and when — not:

attached relationships as r implies r.count =
    old (if attached relationships as o then o.count + 1 else 1 end)

The intuition behind the assertion is as follows:

  1. If relationships is Void on exit, we do not care about count.
  2. If relationships is attached on exit, it has one more item compared to the old value. The old value may be attached or Void:
    • if the old value is attached, the new number of items is the old number plus 1;
    • if the old value is Void, the new number of items is 1.
Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • Does that really work? Don't you need to `twin` the "old" relationship, otherwise it's the same object as the contemporary one and therefore they would have the same count. – Eric Bezault Jan 13 '19 at 20:57
  • @EricBezault Indeed, I've corrected the code accordingly. – Alexander Kogtenkov Jan 13 '19 at 21:06
  • Seems pretty cryptic for me... to what the old does refer to in evaluation of your statement between parenthesis? either "new" relationships.count + 1 or 1? I'd like to compare relationships.count with old relationships.count don't I? – Pipo Jan 24 '19 at 15:25
  • According to your point, the new following statement would be v valid? compilation succeds, but runtime complains on `relationships_added_old_attached``relationship_added_old_void: not attached old relationships_from_data implies attached relationships_from_data as l_new_rel and then l_new_rel.count = 1 relationships_added_old_attached: attached old relationships_from_data as l_old_rel implies attached relationships_from_data as l_new_rel and then l_new_rel.count = l_old_rel.count + 1` – Pipo Jan 24 '19 at 15:30
  • @Pipo As pointed out by Eric, you would need to use `twin` in this code, because `old relationships_from_data` and `relationships_from_data` is the same reference. The code in the answer has the semantics you are looking for, but is much more concise and efficient. – Alexander Kogtenkov Jan 24 '19 at 18:48
  • 1
    @Pipo I've extended the answer with an explanation. – Alexander Kogtenkov Jan 25 '19 at 07:33