2

I am trying to implement does_key_exist, I thought the code below would do it. But I get a compilation error key not found line: Result := not (x.key = key)

does_key_exist(key: attached STRING):BOOLEAN
    do
        Result := not data.item(hash(key)).for_all (agent (x:like pair_at):BOOLEAN
            do
                Result := not equal(x.key, key)
            end)
    end

definitions:

pair_at(key:attached STRING):TUPLE[key:STRING;value: like value_at]
    require
        does_key_exist(key)
    deferred
    ensure
    end

list_at(key:STRING) : LINKED_LIST[like pair_at]
        require
            does_key_exist(key)
        end

data : ARRAY[like list_at]
ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52

1 Answers1

2

Inline agents in Eiffel have access to attributes of the current object, but not to locals or arguments, as they are just syntactic sugar for "normal" agents, that are constructed upon normal features of the class. The latter do not have any access to the locals or arguments of the other features. So the code can be corrected by passing the argument explicitly:

does_key_exist (key: STRING): BOOLEAN
    do
        Result := not data.item (hash (key)).for_all
            (agent (x: like pair_at; y: STRING): BOOLEAN
                do
                    Result := not equal(x.key, y)
                end
            (?, key))
    end
ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52
Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • it works (had to fix a bug in equality test, need object not reference equality) – ctrl-alt-delor Jun 20 '12 at 19:23
  • Is the (hidden) argument list, just (?) ? I will look at the manual again. – ctrl-alt-delor Jun 20 '12 at 19:25
  • 1
    @richard, if there is no explicit argument list, all the arguments are considered open. This is equivalent to adding an explicit argument list with as many question marks as there are formal arguments in the agent declaration. Of course, if all the arguments are open, it's more convenient to omit the explicit list of the arguments altogether. – Alexander Kogtenkov Jun 21 '12 at 06:57