-1

Is the Result keyword automatically recognized as the return value/object?
What is the proper syntax to be used?
Unfortunately, I cannot find a clear indication from the documentation and the various examples online.

vkoukou
  • 140
  • 2
  • 15

1 Answers1

2

The keyword Result is just a local variable with a reserved name and the ability to use it in a feature body as well as in the corresponding postcondition. The last value attached to Result before exiting the feature is the value returned by this feature. Here is an example:

foo: SOMETHING
    do
        Result := bar
        if Result.whatever then
            qux (Result)
        else
            something_else := Result
            Result := some_other_value
        end
    ensure
        valid_result: Result.is_valid
    end

There is a validity rule that states that Result can be used only in features that return a value, because it has no meaning in procedures that do not return anything.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • Thanks for the answer Alexander!Can the Result also return an object? – vkoukou Nov 08 '17 at 15:04
  • @vkouk, what do you mean by "return an object"? The expressions `bar` and `some_other_value` are evaluated at run-time and produce some object that is attached to `Result`. This object is then returned by the function `foo`. All values in Eiffel are objects. – Alexander Kogtenkov Nov 08 '17 at 15:10
  • I thought there were also native types as in other high-level languages, now I understand. Thanks a lot for the answer! – vkoukou Nov 08 '17 at 15:15
  • @vkouk, indeed, there are types the compiler knows about in advance. They are required to handle literal values such as `True` or `5`. Other than that all types follow the same set of rules. So, `True` denotes an instance of an expanded class `BOOLEAN`, `5` denotes an instance of an expanded class `INTEGER`, `"ABC"` denotes an instance of a reference class `STRING`, etc. All these instances are objects. – Alexander Kogtenkov Nov 08 '17 at 15:24