Search code examples
syntaxeiffel

How to return a value or an object in Eiffel?


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.


Solution

  • 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.